Class Benhang extends card{/ * Constructors and constructs inherit */function __construct ($cardno, $pwd, $name, $money) { parent::__ Construct ($cardno, $pwd, $name, $money); } function Take ($money) { echo ' bank withdrawal {$money} No handling fee <br> "; } function Zhuan ($money) { echo ' bank transfer {$money} <br> "; } } $benhang =new Benhang (123,344,444,444); $benhang->check (); $benhang->take (234); $benhang->zhuan (4555);/* Other bank card Classes */class Qita extends Card{function __construct ($cardno, $pwd, $name, $money) { parent::__construct ($cardno, $pwd, $name, $money); } function Take ($money) { echo ' non-bank withdrawal {$money} There is a handling fee of $2 <br> "; }} $qita =new Qita (123,344,444,444); $qita->check (); $qita->take (99);
three main features of PHP object-oriented: inheritance, encapsulation, polymorphism
I. Inheritance
1, how to achieve inheritance?
Use the extends keyword for subclasses to allow subclasses to inherit the parent class;
class Student extends person{}
2, to realize the attention of inheritance?
The ① subclass can inherit only the non-private properties of the parent class.
After the ② subclass inherits the parent class, it is equivalent to copy the parent class's properties and methods to the subclass, which can be called directly using the $this.
③php can only be single-inherited and does not support a class inheriting multiple classes. But a class carries out multiple layers of inheritance;
class person{}
Class Chengnian extends person{}
class Student extends chengnian{}
The //student class has both the properties and methods of the Chengnian class and the person class
3. Method overrides (method overrides)
The conditional ① subclass inherits the parent class.
The conditional ② subclass overrides the existing method of the parent class.
Meet the above two conditions, called method overrides. After overwriting, the subclass calls the method, and the subclass's own method is called.
Similarly, in addition to method overrides, subclasses can have properties with the same name as the parent class, overriding the property.
4, if the subclass overrides the parent class method, how to call the parent class method with the same name in the child class?
Partent:: Method Name ();
Therefore, when the subclass inherits the parent class, the first step in the construction of the subclass is to call the parent class construct to replicate.
function __construct ($name, $sex, $school) {
Parent::__construct ($name, $sex);
$this->school = $school;
}
Example one:
class person{protected $name;p ublic $sex; function __construct ($name, $sex) {//declaration constructor $this->name = $name; $this->sex = $sex; } function say () {echo ' My name {$this->name}, I am {$this->sex} born! <br> "; }} class Student extends person{//subclass inherits the parent class public $school; function __construct ($name, $sex, $school) {//Subclass constructor Parent::__construct ($name, $sex); Call the parent class construct to replicate $this->school = $school; } function Program () {echo "PHP is really fun! I love php!. PHP is the best programming language in the world! <br> "; } function Say () {Parent::say (); Overriding the method with the same name as the parent class echo "I am {$this->school}"; }} $zhangsan = new Student ("Zhang San", "Male", "set sail"); $zhangsan->say (); $zhangsan->program ();
Second, the package
1. What is encapsulation? by accessing modifiers, properties and methods in the class that do not require external access are privatized to implement access control. "Note": access control is implemented, not access denied. That is, after we privatize the attribute, we need to provide a corresponding method to let the user handle the property through the methods we provide. 2, the role of encapsulation? ① users only care about the functionality that the class can provide, without having to worry about the details of the feature implementation! (Encapsulation method) ② controls the user's data, prevents the setting of illegal data, and controls the data returned to the user (attribute encapsulation +set/get method) 3. Implementation of encapsulation operation? Encapsulation of the ① method for some methods that are only used inside the class, rather than being used externally. So, we can use private to do the privatization process. Private Function FormatName () {} //This method can only be used within the class using $this call function ShowName () { $this-FormatName (); } Encapsulation +set/get method for ② properties to control the setting and reading of attributes, properties can be privatized and required to be set by the Set/get method we provide private $age; function Setage ($age) { $this->age = $age; } function Getage () { return $this->age; } $ object, Getage (); $ object, Setage (n); Encapsulation of ③ attributes + Magic method private $age; function __get ($key) { return $this $key; } function __set ($key, $value) { $this $key = $value; } $ object->age; The __get () Magic method is called automatically when accessing the object's private property, and the name of the accessed property is passed to the __get () method; $ object->age=12; when setting the object's private property, the __set () Magic method is called automatically, and the Set property name and the property value are passed to the __set () method; "Note": in the Magic method, you can use the branch structure, to determine the different $key, to do different operations. 4, about the Magic method of encapsulation: ①__set ($key, $value): called automatically when assigning a value to a class private property, passing two arguments to a method when invoked: the property name and the property value to be set; ②__get ($key): automatically called when the class private property is read, passing a parameter to the method when invoked: the name of the property to be read; ③__isset ($key): called automatically when a private property is detected externally using the Isset () function. use Isset () outside the >>> class to detect private properties, which are not detected by default. False >>> So, we can use the __isset () function, which returns the internal detection results when automatically called. function __isset ($key) { return Isset ($this-$key); } when externally using the Isset ($ object name-and private property), the results returned by the above __isset () are automatically invoked when detected! ④__unset ($key): The external use of the unset () function when the private property is deleted, automatically called; function __unset ($key) { unset ($this, $key); } when external uses unset ($ object name-and private property), the property name is automatically passed to __unset () when the property is deleted, and is handled by this magic method.
example of a
Class Person{public $name;p ublic $age;p ublic $sex; function __construct ($name, $age, $sex) {$this->name= $name; $this->setage ($age); $this->setsex ($sex); } function Setage ($age) {if ($age >=0&& $age <=120) {return $this->age= $age; }else{die ("wrong age input!!! "); }} function Setsex ($sex) {if ($sex = = "female" | | $sex = = "Male") {return $this->sex= $sex; }else{die ("wrong sex input!!! "); }} function say () {echo "My name is {$this->name}, my age {$this->age}, my gender is {$this->sex}<br> ;"; } } Class work extends Person{private $position; function __construct ($name, $age, $sex, $position) {parent::__construct ($name, $age, $sex); $this->job= $job; $this->setposition ($position); } function SetPosition ($position) {$arr =[' director ', ' chairman ', ' Programmer ', ' cleaner '];if (In_array ($position, $arr)) {return $this->position= $position; }else{die ("does not exist in this position"); }} function __set ($key, $value) {if ($key = = "Age") {return parent::setage ($value); } elseif ($key = = "Sex") {return parent::setsex ($value); } elseif ($key = = "position") {return $this->setposition ($value); }return $this $key = $value; } function Say () {Parent::say (); echo "My position is {$this->position}"; }} $zhangsan =new work ("Zhang San", 22, "Male", "director"); $zhangsan->setsex ("female"); $zhangsan->setage ()//$zhangsan->setposition ("chairman"), $zhangsan->position= "Chairman"; $zhangsan->name= "Lisi"; $zhangsan->say ();
three. Polymorphic 3.1, what is polymorphic? the precondition for polymorphic polymorphism is to implement inheritance. 1. A class is inherited by multiple subclasses, and if a method of this class shows a different function in multiple subclasses, we call this behavior polymorphic. In PHP, the method overrides, 2. The necessary ways to achieve polymorphism: ⑴ Subclass inherits the parent class; ⑵ overriding the parent class method; ⑶ The parent class reference to the Child class object;
/* Cartridge Interface * Paper interface */interface inkbox{ function color ();} Interface paper{ function sizes ();} Class Computer{function Fangfa (Inkbox $a, Paper $b) { //Parent reference echo "About to start printing" <br> "; $a->color (); $b->sizes (); echo "Print end ... <br> "; }} Class Color implements Inkbox{function color () {echo "Loading color cartridge <br>"; echo "Implement color Cartridge <br>"; }} Class White implements Inkbox{function color () {echo "Loading monochrome cartridge <br>"; echo "Implement monochrome cartridge <br>"; }} Class A4 implements Paper{function sizes () {echo "Loading A4 paper <br>"; echo "Implementation A4 paper <br>"; }} Class A5 implements Paper{function sizes () {echo "Implements A5 paper <br>"; }} $com =new computer ();//Create Object $com->fangfa (new Color (), New A4 ());//Sub-class object