PHP Object-oriented explanation: inheritance, encapsulation and polymorphism

Source: Internet
Author: User

First, explain what is object-oriented before interpreting object-oriented?

[Object-oriented]
1. What is a class?
A collection of a series of individuals with the same attributes (characteristics) and methods (behaviors), which are abstract concepts
2. What is an object?
A specific attribute that is obtained from a class is worth an individual, called an object, and the object is a specific individual.

So, object-oriented is that we focus on the object to deal with the problem by getting the object from a class with attributes and functions.

Now let's go over the three main features of object-oriented: inheritance /encapsulation/polymorphism

First, inheritance

In PHP we mainly Customs extends keyword to achieve inheritance,class Student extends person{}

Here are a few of the more important considerations:

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 properties and methods of the parent class to the subclass, which can be called directly using $this;
③php can only be single-inherited and does not support a class inheriting multiple classes. However, a class can inherit multiple layers (that is, a inherits from B, and C inherits from A,c indirectly inherits B).

Second, the package

Class implementation encapsulation is to not allow the outside class to arbitrarily modify the member variables of a class, so when defining a member of a class, we use the Private keyword to set the access rights of this member

Can only be called by other member methods of this class, not by methods in other classes, that is, through the methods provided in this class to access private properties in this class.

① so in this class we will provide a way to access the private property

② then we will generally define two methods to implement the operation of a variable, namely:get () and set () method.

The code examples are as follows:

classperson{Private $name; Private $age;  Public function__construct ($name,$age){            $this->name =$name; $this->age =$age; }        functionSetage ($age){            if($age>=0&&$age<=120){                $this->age =$age; }Else{                Error_log("Wrong Age setting!") "); }        }                functionGetage () {return $this-Age ; }         Public functionsay () {Echo"My name is {$this->name}, this year I {$this->age} years old. "; }                function__get ($name){            Switch($name) {                 Case' Name ':return $this-$name." This is the text added when reading ";  Case' Age ':return"0".$this-$name; default:return $this-$name; }                    }        function__set ($key,$value){                        if($key= = "Name"){                $this-$key=$value." This is the text that is added when setting <br> "; }Else{                $this-$key=$value; }        }        function__isset ($name){            return isset($this-$name); }                function__unset ($name){            if($name= = "Age"){                return; }             unset($this-$name); }    } $zhangsan=NewPerson ("Zhangsan", 14);$zhangsan->setage (12);Echo $zhangsan->getage (). " <br> ";Var_dump(isset($zhangsan-name));unset($zhangsan-Age );Echo $zhangsan->age;

Three, polymorphic

What is polymorphic?

A class, inherited by multiple subclasses, if a method of this class, in multiple subclasses, shows different functions, we call this behavior polymorphic. (different subclasses of the same class exhibit different patterns)

So how do we achieve polymorphism?

Subclass inheriting parent class, subclass overriding parent class method, parent class reference to child class object

abstract class person{        NOTE: The parent class uses the abstract keyword decoration
Span style= "COLOR: #ff6600" >abstract function Say ();
}

class Chinese extends person{    NOTE: Subclass overrides parent class method
function say () {
echo "I am Chinese <br>";
}
}
class 中文版 extends person{    Note: Subclass overrides parent class method
function say () {
Span style= "COLOR: #ff6600" >echo "I am English";
}
}

$zhangsan = new Chinese ();
$zhangsan->say ();
$z = new 中文版 ();
$z->say ();
Person $p = new Chinese (); Note: The parent class reference points to the child class object

In the code above, two subclasses inherit from the same parent class, but because all of the methods of the parent class are overridden, they show different patterns.

* Four, single case design mode


The singleton mode is also called a single-state mode
You can guarantee that a class can have only one instance of an object
Key points of implementation:
The ① constructor is privatized and does not allow objects to be created with the New keyword.
② provides methods for getting objects, determines whether an object is empty in a method, creates an object if it is empty, and returns if it is not empty.
The properties of the ③ instance object and the method that gets the object must be static.
After ④, creating objects only uses the static methods we provide.

The sample code is as follows:

classsingleton{Static  Public $Single=NULL;Private function__construct () {}Static functionGetsingle () {if(!self::$Single) { self::$Single=NewSingleton ();//the self refers to the class name New Singleton () and newself () are exactly the same}returnSelf::$Single;}function__destruct () {Echo"I was destroyed <br>";}} $s 1= Singleton::Getsingle ();$s 1= Singleton::Getsingle ();$s 1= Singleton::getsingle ();

PHP Object-oriented: inheritance, encapsulation, and 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.