13. Access type The
type access modifier allows developers to restrict access to class members. This is 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 three access modifiers:
public (public, default), private (private) and protected (protected).
public public modifier, members in the class will have no access restrictions, all external members can access (read and write)
This class member (including member attributes and member methods), in all versions before PHP5 In PHP, all members of a class are
public, and in PHP5, if a member of a class does not specify a member access modifier, it will be considered public.
Example: public $name;
public function say(){};
private private modifier, defined as a private member, is visible to all members in the same class, that is,
There is no access restriction; but external code of this class is not allowed to change or even read operations, and subclasses of this class cannot access private-modified members.
Example: private $var1 = 'A'; //Attribute
private function getValue(){} //Function
protected protected member modifier, members modified as protected cannot be accessed outside the class code access. However,
has access rights to subclasses of this class and can read and write properties and methods. External code of this subclass, including its
subclasses, do not have permission to access its properties and methods.
Example: protected $name;
protected function say(){};
private protected public
In the same class√ √ √In subclasses of
class√ √
All External member√
Code snippet
Copy code The code is as follows:
/* *
* 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 not private
protected $protected = 'Protected2';
function printHello(){
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
$obj2 = new MyClass2();
echo $obj->public; //Works
echo $obj2->private ; // Undefined
echo $obj2->protected; // Fatal Error
$obj2->printHello(); // Shows Public, Protected2, not Private
?>
Code snippet
Copy code The code is as follows:
/**
* Define MyClass
*/
class MyClass{
// Constructors must be 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 is 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 is public
function Foo2(){
$this->MyPublic();
$this->MyProtected();
$this->MyPrivate(); // Fatal Error
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Private
?>
In addition, when a subclass overrides a method of a parent class, you should also pay attention to it. The access permission of the method in the subclass must not be lower than that of the parent class
The access permission of the overridden method must be higher than or equal to the parent class. Access permissions for class methods.
For example, if the access permission of the parent class method is protected, then the permissions to be overridden in the subclass must be protected
and public. If the parent class method is public, then the method to be overridden in the subclass can only be public. , in short, the
method in the subclass must always be higher than or equal to the access rights of the overridden method in the parent class.
http://www.bkjia.com/PHPjc/320635.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320635.htmlTechArticle13. Access type The access modifier allows developers to restrict access to class members, which is PHP5 New feature, but a good feature of the OOP language. And most O...