PHP encapsulation, inheritance, polymorphism

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.