php-basics-revision [topic-name: Class/object,constructor,destructor,access modifiers]
here we will start with basic class of object orient programming in php.
Class , properties , methods , instance :
<?php
class Fruit{
//class properties
public $name ; //property-1
public $color ; //property-2
// Methods
// method-1 this is a setter method
// we will assign a value in public $name property using
// this set_name() setter method
public function set_name($name){
$this->name = $name ; //about $this keyword
// $this keyword is targeting parent scope of set_name()
// method or Current Objects property public $name
// and equal sign is assigning
// value of $name parameter
// to targeted public $name property
}
//method-2 this is a getter method
//we will get or retrive the value of public $name property
// using this get_name() getter method
function get_name() {
return $this->name;
}
}
$f1 = new Fruit(); //creating instance of Fruite Class named $f1
//now $f1 get all access of properties and methods of Fruite
//Class
$f1->set_name('sakura plum'); // setting public $name property
//value using set_name setter method and value is "sakura plum"
echo($f1->name); //here we accessing the value of public $name
//property using instance named $f1 of Fruite Class
// note we can access directly public $name value if property
//is public if property is private like private $name then we
//must need a getter_method to access the value
//of private $name property
echo( $f1->get_name()); // here we are getting property value
//of $public name using get_name() getter method
//here is example for more then one property value assign
//using one setter method is same Fruite class
class Fruit{
//class properties
public $name ;
public $color ;
//methods
function set_name($name , $color)
{
$this->name = $name; //setting name
$this->color = $color ; //setting color
}
function get_name()
{
//getting values of name and color
return "The Name Is :".$this->name. " And The Color Is : ".$this->color;
}
}
$f1 = new Fruit(); //instance 1 for Fruite Class $f1 have all
//properties and methods that Fruite Class have
//we don't need to write Fruite Class properties and methods
//again we can reuse Fruite Class as needed
//this is the super power of Object orient programming
$f2 = new Fruit(); //instance 2 for Fruite Class $f2 have all
//properties and methods that Fruite Class have
$f1->set_name('sakura plum','indigo');
//$f1 blue print for Fruite
echo( $f1->get_name());
$f2->set_name('blue berry','navy blue');
//same named classes blue print for $f2
echo($f2->get_name());
echo($f2 instanceof Fruit); //this line will return 1
// instence of will check if $f2
//is instance or not of Fruite Class
echo($f2 instanceof Fruitt); //this line will return nothing
?>
__construct and __destruct methods oop in php:
class Fruit
{
public $name;
public $color;
//__construct() method is allow us to initialize an
//object's properties when we create the instance of
//class with out write setter_method
//__contruct method starts with two under score
function __construct($name, $color)
{
$this->name = $name ;
$this->color = $color ;
}
// __distruct method if we create a distruct method
// php will call it
// automatically at the end of the script
function __destruct(){
echo "The Fruit Name is {$this->name} and the fruit color is {$this->color}";
}
}
$f1 = new Fruit('sakura plum','indigo');
$f2 = new Fruit('blue berry','navy blue');
//we don't need to echo to view the properties current value
access modifiers in php:
public
- the property or method can be accessed from everywhere. This is defaultprotected
- the property or method can be accessed within the class and by classes derived from that classprivate
- the property or method can ONLY be accessed within the class
<?php
class Fruit
{
public $name ;
protected $color ;
private $weight ;
public function set_name($name){
$this->name = $name ;
}
public function set_color($color){
$this->color = $color ;
}
public function set_weight($weight){
$this->weight = $weight ;
}
public function get_color(){
return $this->color ;
}
public function get_weight(){
return $this->weight ;
}
}
$mango = new Fruit() ;
$mango->name = "mm"; //will not give a error
//but
//$mango->color = "col"
// $mango->weight = 1010
// will throw a error because color is protected and
// weight is private property to set a value in
// color and weight we need a setter method
// and when we want to access the value of
// color and weight , we need e getter method
$mango->set_name('mango');
$mango->set_color('green');
$mango->set_weight('1010');
echo($mango->name); // name can be access because its public
echo($mango->get_color());
echo($mango->get_weight());
?>