This tutorial contains some parts of tuorial
Object Oriented PHP - Class and object
Object Oriented PHP - Interface and abstractor
Object Oriented PHP - Constructor and destructor
Object Oriented PHP - Inheritance
Object Oriented PHP - Visivility
Object Oriented PHP - Some common keywords
some magic words in php
Visivility of PHP variable
In php threre has three keyword to visible or not visiable a class properties.
- Public
- private
- protected
public : Public can access from anywhere from the script. It can access inside or ouside of the class.
Private : Private can accesss from the entire class. Not outside of the class or inherited class.
Protected : protected can access from the entire class and inherited class.
By default, all class members are public. This means if you do not declare a property or method within the class, it will be public. It is good practice to declare the visibility of class members for the sake of readability for yourself and others. It is much easier for another programmer to see your intentions. This will also future proof your scripts should access modifiers be deprecated.
Public class members (properties and methods) are available through-out the script and may be accessed from outside the class as we demonstrated in our car class. Lets create a simple new class to demonstrate class properties visibility.
See the exampe to declare visibility for class properties .
<?php class Vehicle{ const constant="Vehicle Shop"; public $brand="Motorola"; private $color="Black"; protected $price="$1200"; function shop(){ echo "Welcome to ".$this->color." vehicle"; } public function showPrice(){ echo "Price : ".$this->price; } private function showBrand(){ echo "Brand : ".$this->brand; } } ?>Now create an object to see that waht can access outside the class or inside the class.
<?php class Vehicle{ public $brand="Motorola"; private $color="Black"; protected $price="$1200"; function shop(){ echo "Welcome to ".$this->color." vehicle"; } public function showPrice(){ echo "Price : ".$this->price; } private function showBrand(){ echo "Brand : ".$this->brand; } } $class = new Vehicle(); echo $class->shop()."\n"; echo $class->showPrice()."\n"; echo $class->showBrand()."\n"; // the showBrand() is not accessible outside the class ?>Abobe example create some properties with visibility. Public functions are access from outside the class. Create an object of vehicle class and access public fuction. But here can not access the private function "showBrand()". The all Public functions can access from anywhere in this script. But protected prperties can not access outside the class, but can access from child class. the private is not access outside class.
<?php class Vehicle{ public $brand="Motorola"; private $color="Black"; protected $price="$1200"; function shop(){ echo "Welcome to ".$this->color." vehicle"; } private function showPrice(){ echo "Price : ".$this->price; } protected function showBrand(){ echo "Brand : ".$this->brand; } } class Bike extends Vehicle{ function info(){ echo "Inforamtion of Vehicle.\n"; } } $class = new Bike(); echo $class->shop()."\n"; //echo $class->showPrice()."\n"; //Fatal error: Call to private method Vehicle::showPrice() from context '' in E:\taslim server\EasyPHP-5.3.3.1\www\login\visible1.php on line 28 echo $class->showBrand()."\n"; //Fatal error: Call to protected method Vehicle::showBrand() from context '' in E:\taslim server\EasyPHP-5.3.3.1\www\login\visible1.php on line 31 echo $class->info()."\n"; ?>Now create another example to see it clearly. For this we need to learn PHP inheritance. If you don't know how inheritace works click here
<?php class Vehicle{ public $brand="Motorola"; private $color="Black"; protected $price="$1200"; function shop(){ echo "Welcome to ".$this->color." vehicle"; } private function showPrice(){ echo "Price : ".$this->price; } protected function showBrand(){ echo "Brand : ".$this->brand; } } class Bike extends Vehicle{ function info(){ echo "Inforamtion of Vehicle.\n"; parent::shop(); parent::showPrice(); //Fatal error: Call to private method Vehicle::showPrice() from context 'Bike' in E:\taslim server\EasyPHP-5.3.3.1\www\login\visible6.php on line 24 parent::showBrand(); } } $class = new Bike(); echo $class->info()."\n"; ?>
This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. PHP tutorials
ReplyDelete