Learn PHP

PHP class access control

Access control modifiers can place before a class, a method definition, or a property to alter the way PHP treats them.

Access Modifiers Meaning
Public Set property or method to be accessible from anywhere
Private Set property or method to be accessible only by its own class or object
Protected Set property or method to be accessible by its class or by its descendants
Final Set property, method, or class cannot be overridden in subclasses
Abstract Set method or class cannot be used directly. We have to subclass it.

Most of the variables in a class should be marked as either protected or private.


Public properties and methods are accessible from anywhere.

	      		class Book {
			        public $Name;
			        public function say() {
			                print "PHP!\n";
			class PythonBook extends Book {
			        public function say() {
			                print "Python!\n";

			$aBook = new PythonBook;
			$aBook->Name = "Python";
			print $aBook->Name;


Private properties are accessible only inside its own the class.

	      		class Book {
			        private $Name;
			        private $NameTag;
			        public function setName($NewName) {
			                // etc

Child classes cannot access private parent methods and properties. If you want to do this, you need the protected keyword instead.


Protected properties and methods are accessible within its owns class and a child class. Consider the following code:

	      		class Book {
			        public $Name;
			        protected function getName() {
			                return $this->Name;

			class Poodle extends Book {
			        public function say() {
			                print "'Book', says " . $this->getName();

			$aBook = new Poodle;
			$aBook->Name = "PHP";


			  class Widget
			    private $name;
			    private $price;
			    private $id;

			    public function __construct($name, $price)
			      $this->name = $name;
			      $this->price = floatval($price);
			      $this->id = uniqid();

			    //checks if two widgets are the same
			    public function equals($widget)
			      return(($this->name == $widget->name) AND
			       ($this->price == $widget->price));

			  $w1 = new Widget('Cog', 5.00);
			  $w2 = new Widget('Cog', 5.00);
			  $w3 = new Widget('Gear', 7.00);

			    print("w1 and w2 are the same<br>\n");

			    print("w1 and w3 are the same<br>\n");

			  //FALSE, == includes id in comparison
			  if($w1 == $w2)
			    print("w1 and w2 are the same<br>\n");

PHP Tutorials






Echo and Print Statement

Data Types



Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion

file i/o