<?php
Three major features: encapsulation, inheritance, polymorphism
Encapsulation Practices:
1. Change member variables to private
2. Doing methods in a class indirectly manipulating member variables
3. Control conditions in the method Riga
Encapsulation Purpose: Make classes more secure
/* Class ren{
Public $name;
private $age;//change the variable to private */
The first method of doing
Public function Setage ($a) {//Assignment
if ($a >=18 && $a <=50) {
$this->age = $a;
}else{
echo "The age is not within the range";
}
}
Public Function Getage () {//value
return $this->age;
}
Generic method indirect Operation member variable
Magic method to assign values to variables
Public Function __set ($k, $v) {//Do a function indirect operation
$this $k = $v;
}
The Magic method of assigning variable value
Public Function __get ($k) {
return $this $k;
}
}
$s = new Ren ();//The first object created
Assignment, value output of the first method
$s->setage (18);
Var_dump ($s);
echo $s->getage ();
Assignment, value output (Universal) for the first method
Encapsulation of the first assignment output
/* $s->__set ("age", 20);
Var_dump ($s); */
Encapsulation of the second assignment output
/* $s->age = 20;
Var_dump ($s); */
Value output
/* $s->age = 20;
Echo $s->age; */
Object-oriented instances
Eg: a large circle contains a small circle, a circle radius of 10, a small circle radius of 5, the area of the circle minus the small circle.
Class circle{
Public $r;
Public $area;
Public $cir;
Public Function area () {
return 3.14* $this->r* $this->r;
}
Public Function Cir () {
return 2*3.14* $this->r;
}
Public function __construct ($r) {
$this->r = $r;
}
}
$r 1 = new Circle (10);//Create First Circle
$r 2 = new Circle (5);//Create a second circle
$area = $r 1->area ()-$r 2->area ();
echo "$area <br>";
$cir = $r 1->cir ()-$r 2->cir ();
Echo $cir;
?>
Three features of PHP object-oriented operation-encapsulation, inheritance, polymorphism (upper)