13. Type of Access
The access modifiers of a type allow developers to restrict access to class members, a new feature of PHP5, but
is a good feature of the OOP language. And most OOP languages already support this feature. PHP5 supports the following 3 types of visits
Ask Modifiers
Public (common, default), private (proprietary) and protected (protected) three species.
Public-owned modifiers, the members of the class will not have access restrictions, all external members can access (read and write)
This class member (including member properties and member methods), in all versions prior to PHP5, the members of the class in PHP are
Public, and in PHP5 if a member of a class does not specify a member access modifier, it is considered public.
Example: public $name;
Public function Say () {};
Private personal modifier, defined as a member of private, is visible to all members of the same class, i.e.
There is no access restriction, but the external code for the class is not allowed to change or even read, and for subclasses of that class, it is not
Members that can access private adornments.
Example: private $var 1 = ' A '; Property
Private Function GetValue () {}//function
Protected protects member modifiers, the members that are decorated as protected cannot be accessed by external code of the class. But
With access to subclasses of the class, you can read and write properties, methods, and the outer code of the subclass includes its
Subclasses do not have permission to access their properties and methods.
Example: protected $name;
protected function Say () {};
Private protected public
√√√ in the same class
√√ in subclasses of classes
All external members √
Code fragment
Copy Code code as follows:
<?php
/**
* Define MyClass
*/
Class myclass{
Public $public = ' public ';
Protected $protected = ' protected ';
Private $private = ' private ';
function Printhello () {
Echo $this->public;
Echo $this->protected;
Echo $this->private;
}
}
$obj = new MyClass ();
Echo $obj->public; Works
Echo $obj->protected; Fatal Error
Echo $obj->private; Fatal Error
$obj->printhello (); Shows public, Protected and Private
/**
* Define MyClass2
*/
Class MyClass2 extends myclass{
We can redeclare the public and protected method, but is not private
protected $protected = ' Protected2 ';
function Printhello () {
Echo $this->public;
Echo $this->protected;
Echo $this->private;
}
}
$obj 2 = new MyClass2 ();
Echo $obj->public; Works
Echo $obj 2->private; Undefined
Echo $obj 2->protected; Fatal Error
$obj 2->printhello (); Shows public, Protected2, not Private
?>
Code fragment
Copy Code code as follows:
<?php
/**
* Define MyClass
*/
Class myclass{
Contructors must is public
Public Function __construct () {}
Declare a public method
Public Function Mypublic () {}
Declare a protected method
protected function myprotected () {}
Declare a Private method
Private Function Myprivate () {}
This are public
function Foo () {
$this->mypublic ();
$this->myprotected ();
$this->myprivate ();
}
}
$myclass = new MyClass;
$myclass->mypublic (); Works
$myclass->myprotected (); Fatal Error
$myclass->myprivate (); Fatal Error
$myclass->foo (); Public, Protected and Private work
/**
* Define MyClass2
*/
Class MyClass2 extends myclass{
This are public
function Foo2 () {
$this->mypublic ();
$this->myprotected ();
$this->myprivate (); Fatal Error
}
}
$myclass 2 = new MyClass2;
$myclass 2->mypublic (); Works
$myclass 2->foo2 (); Public and Protected work, not Private
?>
Also note that when subclasses override the parent class, the access rights of the methods in subclasses must not be lower than the parent class
The access rights of the overridden method, that is, must be higher than or equal to the access rights of the parent class method.
For example, if the access permission for the parent class method is protected then the permission to be overridden in the subclass is protected
and public, if the method of the parent class is public then the method to overwrite in the subclass can only be public, in general, in the child class
method is always higher than or equal to the access rights of the method overridden by the parent class.