Package and inheritance of PHP class

Source: Internet
Author: User
Tags php class

Packaging

Encapsulating member methods and member properties into a class, hiding the details of the properties and methods implementation, and restricting access to the members of the class, such as public, protected, private, and so on, the data is protected internally, only by the authorized member method, and the members are encapsulated as much as possible.

Public: The method or property can be accessed under any scope, and is the default, and it will be public if no access modifier is specified for a property or method.
Protected: This class and subclass can be accessed, and external objects cannot be called.
Private: can only be accessed in this class, and it is not possible for subclasses and external objects to be called. A method or property that is marked by private can be redefined in an inherited class, and each class can see only its own private methods that it defines.

These three modifiers should be sorted from large to small in terms of scope: Public→protected→private is scoped, because the class encapsulates properties and methods, which determines the "visibility" of the data, and In this way we can not arbitrarily modify the properties and methods defined on the outside of the class and only be able to invoke, this is the benefits of encapsulation, but also improve security.
We give the code example:

?
123456789101112 class myClass{  public $public="Public";    //public属性 protected $protected="Protected"; //protected属性 private $private="Private";   //private 属性 function say_Hello() {    //public属性  //只是举例说明,自行添加内容 } $obj=new myClass(); echo $obj->public; //echo $obj->protected; //echo $obj->private;

By running the example above we get a "public", but when you remove//echo $obj->private;, you get the following error:

Fatal error:cannot Access protected property MyClass:: $protected the e:apachehtdocsexamplefile.php on line 13.

We can see that we are not able to access the class's property definitions at will, and we do not know in the "outside" that there are members in this class, because these members may not be available for other classes. Of course, if we have to access or modify properties defined as "private," you can also use the system methods provided by PHP: _get () and _set ().

Inherited

You can inherit a class and have another member property and method that already exists for the class, which is called the parent class or base class, and the inheriting class is a subclass. The inheritance relationship is implemented through the extends keyword. In layman's terms, there has to be a "root cause" for inheritance, which you may imagine as having a son or daughter, who will get some "things (properties and methods)" from you, so that your "offspring" is holding all of your (root) traits.

Generate a "root" class (parent or base class)
Syntax: Class father{
}

Produce "Descendants" (subclass)
Syntax: class son extends father{
}

The PHP extends class inherits the sample code:

?
123456789101112131415161718192021222324 class father{  protected $name;  function __construct($name){  //构造函数   $this->name=$name;  }   function work(){   echo "{$this->name}我在工作; }  function __destruct(){}  //析构函数}  class son extends father{  //继承父类 function play(){   echo "{$this->name}我在玩游戏; }   $my_father=new father(“爸爸”);  //创建父类对象 $my_father->work();  $my_son=new son(“儿子”);  $my_son->work();  $my_son->play();

Parsing: In the parent class father, we define the general properties and methods, and then define the subclasses. You may find that there are no constructors and destructors within subclasses, because subclasses are all methods that inherit the parent class, so you can call $my_son->work (); This is the inheritance of the PHP class. Also note: PHP can not be multi-layered inheritance, such as: Class A extends B extends C, such inheritance in PHP is invalid, only single inheritance in PHP, can not inherit more, need other ways to "implement" multiple inheritance.

Package and inheritance of PHP class

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.