What are the object-oriented features of PHP? Introduction to PHP Object-oriented features (code)

Source: Internet
Author: User
In this case, the PHP object-oriented, it should be called PHP Object-oriented programming, PHP three basic features of object-oriented programming are encapsulation, inheritance and polymorphism, let's look at the three main features of PHP object-oriented.

PHP Object-oriented three major features:
1. Package:
Encapsulation is the abstraction of data and the operation of the data is encapsulated together, the data is protected inside, the other parts of the program only through the authorized operation (member method) to manipulate the data.

The Access control assignment value. png

2. Inheritance:
The so-called inheritance is a subclass that inherits some (public/protected) attributes and (public/protected) methods of the parent class through the extends parent class. Cannot inherit from private.

  Role: Increased code reusability and low cost of management code.   Basic syntax: Class  name extends parent class Name {        //required non-private properties and Methods  }

Inherit. png

<?php        //Parent class    Student {public        $name;        protected $age;        protected $grade;        Public __construct () {        //} public        function Showinfo () {            echo $this, name. " <br/> ". $this, age;        }    /**    * Elementary Student *    /class pupil extends Student    {public        function test () {            echo <br/> The pupils are in the exam ... ";        }    }    /**    * University student * *    class Graduate extends Student    {public        function test () {            echo <br/> College students in exams ... ";        }     }    $stu 1 = new pupil ();    $stu 1, name = "Lizzy";    $stu 1, test ();    $stu 1, Showinfo ();    $stu 2 = new graduate ();    $stu 2, name = "ZXM";    $stu 2, test ();    $stu 2-Showinfo ();? >

Attention:
(1) The protected property and method are protected and cannot be called directly in the subclass, and method access is defined in the subclass.

Class ParentClass {public    $name = ' Lizzy ';    protected $age =;    Private $sex = "female";    Public Function test1 () {        echo "<br/> Show Public Method";    }    protected function Test2 () {        echo "<br/> Show protected Method";    }    Private Function Test3 () {        echo "<br/> Show Private Method";    }} Class Subclass extends ParentClass {    function Show () {        echo "age=". $this->age;        echo "sex=". $this->sex;//is not inherited $this,        test2 ();    }} $sub 1 = new Subclass (), $sub 1, Show (), $sub 1, test1 ();

(2) If you want the subclass to call the constructor of the parent class, or another method (public/protected), you can use the class name:: Method name; Or Parent:: Method name;

Class A {public    $n 1=90;    Public Function __construct () {        echo "A's construction method";    }} The construction method of Class B extends A {    function __construct () {        echo "B";        Two methods of calling the parent class        //A::__construct ();        Parent::__construct ();    }} $b = new B ();

(3) When a subclass's method is exactly the same as the parent class method (Protected/public), we call it the override (override) of the method.

3, polymorphic:
"Overloading" is a representation of the polymorphism of a class;
The concept of overloading: like function names, you can call the same function name by the number of arguments or parameter types of the function, but different functions are distinguished.

The Magic function is __call, but is not recommended; It calls a method on an object, and the method does not exist, and the system automatically calls __call.

<?php    Class A {public        function test1 ($p) {            echo "receives a parameter <br/>";        }        Public Function Test2 ($p) {            echo "receives two parameters <br/>";        }        Provided __call it an object calls a method, and the method does not exist, the system automatically calls the __call        function __call ($method, $p) {            if ($method = = "Test") {if                ( Count ($p) = = 1) {                    $this-test1 ($p),                } elseif (count ($p) = = 2) {$this,                    test2 ($p);                }            }        }    }    $a = new A ();    $a, test (1);    $a, test (21,43)?>

Common Magic constants: Two underline line two underline; The current number of lines;
Two underscore file two underline; the absolute path of the current file;

Override/Overwrite (override) of method:
1, when a parent class knows that all subclasses have a method, but the parent class cannot determine exactly how the method is written, you can let subclasses overwrite this method.
Use the following:

<?php    Class Animal {        function Cry () {            echo "does not know how to call";        }    }    Rewrite    Class Dog extends Animal {        function Cry () {            echo "puppy barking";        }    }    Rewrite    Class Pig extends Animal {        function Cry () {            echo "piglet hem";        }    }    $dog = new Dog ();    $dog-Cry ();    $pig = new Pig ();    $pig-Cry ();? >

Details of the method overrides:
1. The function name and number of parameters of the overridden subclass must be the same as the parent class, but not the same as the name of the parameter.
2. If a subclass is going to call a method of the parent class (public/protected), you can use the Parent:: Method name (); or parent class Name:: Method Name (); It is possible to pass the reference according to the situation.
3, the implementation method overrides, the access modifier can be different, but must satisfy the access scope of the subclass >= the access scope of the parent class.

Polymorphic embodiment of the place:
When a subclass does not overwrite a method of the parent class, the child class calls the parent class and calls its own method when the child class overrides the method of the parent class.

Related Article

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.