PHP Object-Oriented programming _1

Source: Internet
Author: User

1, PHP three major features of object-oriented programming:

(1) Encapsulation, encapsulation is the abstraction of the data and the operation of the data encapsulated together, the data is protected inside, the other parts of the program only through the authorized operation (member method) to manipulate the data.

(2) Inheritance, inheritance is a subclass that inherits the (public/protected) attribute and (public/protected) method of the parent class by extends the parent class.

(3) Polymorphism, polymorphism is embodied in the overloading of methods in classes and the coverage of methods.

(4) Abstract, when we define a class, we actually extract the common attributes and behaviors of a kind of things and form a physical model, and the method of studying the problem is called abstraction.

2, encapsulation

Public: The inner, outer, and sub-classes of the class can be accessed;

Protected: The inner and subclass of the class can be accessed;

Private: Only subclasses of the class can be accessed.

If you access protected and private variables outside of the class, you need to use the member method of the public property, such as __get (), __set (), and the member function of PHP is public by default.

3, inheritance

(1) Subclasses can inherit the public and protected properties and methods of the parent class.

(2) A class can inherit only one parent class, and multiple layers of inheritance are required if you want to inherit the properties and methods of more than one class.

(3) When a subclass object is created, the constructor of the parent class is not called by default (this is different from Java).

(4) If the subclass wants to invoke the constructor of the parent class, or another method (PUBLIC/PROTECTD), you can handle the class name: either the method name () or the Parent:: Method Name ().

(5) If the method in the subclass is the same as the parent class, we call the method override or method overwrite.

4, Method overloading

"Overloading" is a manifestation of the polymorphism of a class, which refers to an identifier being used as multiple function names, and the ability to distinguish functions of the same name by the number of arguments or parameter types of a function, without confusion. Call, although the method name is interlinked, but according to the parameter table can automatically call the corresponding function.

Overloading: Like function names, the same function name is called by the number of arguments or parameter types of the function, but different functions can be distinguished.

(1) Although the PHP5 can support overloading, the overloads are more specific to the implementation, and the other languages are significantly different. If you try to define multiple functions with the same name, your program will run with an error. See the following examples:

Class a{public    function test1 () {        echo "test1";    }    Public function test1 ($a) {        echo "test1 hello";    }} $a = new A (); $a->test1 (); $a->test1 (456);

The above examples are for C + +, Java, but not for PHP.

(2) PHP uses magic functions to implement overloading, but this is not recommended.

Class a{public    function Test1 ($p) {        echo "receives a parameter";    }    Public Function Test2 ($p) {        echo "receives two parameters";    } A __call magic function is provided here//when an object calls a method, and this method does not exist, the system automatically calls __call (), takes the method name as the first parameter of __call, and all parameters as the second parameter of the __call.    function __call ($method, $p) {        if ($method = = "Test") {            if (count ($p) = = 1) {                $this->test1 ($p);            } else if (count ($p) = = 2) {                $this->test2 ($p);}}}    $a = new A (); $a->test (1);

  

Summary: About overloading

(1) PHP5 does not directly support method overloading by default.

(2) PHP5 can be used with __call magic method to simulate heavy load effects.

5, Method overrides/method overrides, when a parent class knows that all subclasses have a method, but the parent class is not sure how to write the method, you can let the subclass to overwrite this method.

Class animal{public    function Cry () {        echo ' animal is called ';    }} Class Cat extends animal{public    function Cry () {        echo "cat called";    }} $cat 1 = new Cat (), $cat 1->cry ();

(1) To implement overrides, the method name of the subclass and the parent class is required, and the argument list must be exactly the same.

(2) If the subclass calls a method (public/protected) of the parent class, you can use the Parent:: Method () or the name of the Parental Class:: Method ().

(3) When a subclass overrides a parent class method, it cannot reduce the permissions of the parent class method. There is no error extending the parent class permissions.

When a subclass does not overwrite a method of the parent class, the subclass calls the method of the parent class, and when the child class overrides the method of the parent class, the subclass calls its own method, which also manifests polymorphism.

PHP Object-Oriented programming _1

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.