object-oriented three main features : encapsulation, inheritance, polymorphism.
Packaging
Encapsulate classes to make the class more secure so that some member variables cannot be accessed directly by the outside world.
Method:
1. Change the member variable to private.
2. Make a member method in the class to access it indirectly.
3. Add restrictions to this method
4. Each member variable is encapsulated in the standard case.
classren{ Public $name; Public $sex; Public $birthday; Private $age; Public functionSetage ($age){ if($age>=18 &&$age<=50){ $this->age =$age;} } Public functionGetage () {return $this-Age ; } } $r=NewRen (); $r->name = "Zhang San"; $r->setage (20); Var_dump($r);
Inherited
Concept: Subclasses can inherit everything from the parent class. The parent class plays a guiding role.
Features: Single inheritance
Keyword: extends
class ren{ public$name; Public $sex ; Public function say () { echo "person in Speech"; } } class extends ren{ }$cnew China (); $c->say ();
Overriding: Subclasses can override functions of the parent class. Override
Overloading: One method multiple functions, because the number of parameters is different. Method name is the same, the number of parameters is different. Also called compilation polymorphism.
polymorphism: When a parent class reference points to a subclass instance, because the child class overrides the parent class method, the method is called polymorphic when it is called. Also called Run polymorphic.
PHP encapsulation, inheritance, polymorphism