Encapsulate member methods and member properties into classes,Hide details of property and method implementations,Restrict access to class members through public, protected, private, etc.Data is protected internally,Can only operate through authorized member methods,Encapsulate members as much as possible.

public:methods or properties can be accessed in any scope,And is the default,If no access modifier is specified for a property or method,It will be public.

protected:this class and subclasses are accessible,External objects cannot be called.

private:only accessible in this class,Neither subclasses nor external objects can be called.Privately marked methods or properties can be redefined in inherited classes.Each class can only see its own private methods defined.

These three modifiers should be sorted from large to small like this:public → protected → private is said to be in the scope of effect,Because the class encapsulates some properties and methods,This encapsulation determines the "visibility" of the data, so that we cannot modify the defined properties and methods at will outside the class but only call them.This is the benefit of encapsulation,It also improves security.

We give code examples:

class myclass {
  public $public="public";//public attribute
  protected $protected="protected";//protected attribute
  private $private="private";//private attribute
  function say_hello () {//public attribute
  //Just an example,Add content yourself
 $obj=new myclass ();
 echo $obj->public;
 //echo $obj->protected;
 //echo $obj->private;

We get a "public" by running the above example, but when you uncomment //echo $obj->private;You will get the following error:

fatal error:cannot access protected property myclass ::$protected in e:apachehtdocsexamplefile.php on line 13.

You can see that we can't access the attribute definition of the class at will,We are "outside" not knowing exactly which members are in this class,Because these members may not be available to other classes.Of course, if we must access or modify the properties defined as "private",You can also use the system methods provided by PHP:_get () and _set ().


You can make a class inherit and have member properties and methods of another existing class,The inherited class is called the parent or base class,Inherited classes are subclasses.The inheritance relationship is implemented by the extends keyword.In layman's terms,If there is inheritance, there must be a "root", this "root" you may imagine that you have a son or daughter in the future,They will get some "things (attributes and methods)" from you, so that your "offspring" will have all the characteristics of you (the root).

Generate "root" class (parent or base class)

Syntax:class father {


Produce "offspring" (subclasses)

Syntax:class son extends father {


The php extends class inherits the sample code:

class father {
 protected $name;
 function __construct ($name) {//Constructor
 function work () {
  echo "{$this->name} I am working;
 function __destruct () {} //destructor
class son extends father {//Inherit the parent class
 function play () {
  echo "{$this->name} I am playing a game;
 $my_father=new father ("Daddy");//Create a parent object
 $my_father->work ();
 $my_son=new son ("son");
 $my_son->work ();
 $my_son->play ();

Analysis:In the parent class father, we define general properties and methods,Then define the subclasses.You may find that there is no constructor or destructor in the subclass.Because the child class inherits all the methods of the parent class,So you can call $my_son->work ();This is the inheritance of the php class.Also note:PHP cannot inherit multiple layers,Such as:class a extends b extends c, such inheritance is invalid in PHP,There is only single inheritance in php,Can not inherit moreNeed other ways to "implement" multiple inheritance in disguise.

  • Previous C # structure characteristics example analysis
  • Next mysql update regular replacement sql statement