Access Specifiers

The access specifiers (aka access modifiers) define the level of visibility of a class’s members.

 

The following scope keywords are used to define the level of access:

  • public
    • members can be accessed any place the object is visible, in the script
  • protected
    • members can only be accessed from within the class itself and inherited/inheriting classes
  • private
    • members are accessible only from other members of the same class
    • explicitly; they are not accessible by inherited/inheriting classes

The $this-> pseudo variable must be used with the property name to access specific properties! The use of the member name on it's own will be ignored.

 

Example showing public access:

<?php
class Person {
	public $name ;
	public function speak() {
		echo "Hi, my name is $this->name";
	}
}
$julia = new Person ;
$julia->name = "Julia" ;
$julia->speak() ;
?>

Save & refresh browser:

Hi, my name is Julia

 

To benefit from the essential OO concept of encapsulation (/ data hiding), it is desirable to keep the properties of the class private / protected, and to make the class responsible for setting and getting its own properties via its own methods only, hence disabling unwanted changes in an object's properties by anything other than the object itself.

 

The following example shows a protected property that can be inherited by a child class, if this property was set to private it would not be accessible in the child class and could therefore not be used. The example also shows a private property in the child class that can only be accessed within the class:

<?php
	class Person {
		protected $name ;
		public function setName($myName) {
			$this->name = $myName ; //must use $this-> and the name of the property
		}
		public function getName() {
			return $this->name . "<br>";
		}
		public function speak() {
			echo "Hi, my name is $this->name, and I'm a techie<br>";
		}
	}
	class Scientist extends Person {
		private $toys;
		public function setToys($myToys) {
			$this->toys = $myToys ;
		}
		public function speak() {
			$output = "$this->name is a Scientist who works with $this->toys!!";
			return $output;
		}
	}
	$techie = new Person;
//	$techie->name = "Derrick"; //this causes a fatal error, since the property is protected
	$techie->setName("Derrick");
	echo $techie->getName();
	echo $techie->speak();

	$geek = new Scientist ;
//	$geek->toys = "2DIR spectra" ; //this causes a fatal error, since the property is private
	$geek->setName("Julia") ;
	echo $geek->getName() ;
	$geek->setToys("lazers") ;
	echo $geek->speak() ;

?>

Save & refresh browser:

Derrick
Hi, my name is Derrick, and I'm a techie
Julia
Julia is a Scientist who works with lazers!!

 


final

  • used to to declare that a method or class cannot be overridden by a subclass
  • cannot be applied to properties
  • If the class itself is being defined final then it cannot be extended
  • Means of stopping other programmers using the code in unplanned ways

This example will purposefully fail due to the final keyword being applied to the Person class's speak() function.

<?php
	class Person {
		public $name;
		final public function speak() {
			echo $this->name;
		}
	}
	class Scientist extends Person {
		public function speak() {
			echo "$this->name is a Scientist who works with lazers!!";
		}
	}
	$geek = new Scientist;
	$geek->name = "Julia";
	echo $geek->speak() ;
?>

Save & refresh browser:

Fatal error: Cannot override final method Person::speak() in /root/user/public_html/php/index.php on line 12

 

If the child class had not overridden the parent's final declared method, it would have simply used the parent's method with no errors

 


abstract

  • cannot be used directly
  • has to be inherited by a child class

This examples show an abstract class, Person, being inherited by the child, Scientist, class. When the script tries to instantiate the Parent abstract class a fatal error is returned:

<?php
	abstract class Person {
		public $name;
		public function speak() {
			echo $this->name;
		}
	}
	class Scientist extends Person {
		public function speak() {
			echo "$this->name is a Scientist who works with lazers!!";
		}
	}
	$geek = new Scientist;
	$geek->name = "Julia";
	echo $geek->speak() ;

	$techie = new Person;
	$techie->name = "Derrick";
	echo $techie->speak() ;
?>

Save & refresh browser:

Julia is a Scientist who works with lazers!!
Fatal error: Cannot instantiate abstract class Person in /root/user/public_html/php/index.php on line 17

Leave a Reply