Access control on properties or methods is achieved by adding the keywords public, protected or private in front.
Class members defined by public can be accessed anywhere;
Class members defined by protected can be accessed by subclasses and parent classes of the class in which they are located (of course, the The class in which the member is located can also be accessed); class members defined by private can only be accessed by the class in which they are located.
The following three access modifiers are supported in PHP5.
(1)public. This modifier is the default, if no access modifier is specified for the property or method, it
is public. Public properties or methods can be accessed both inside and outside the class.
(2)private. This modifier indicates that the marked property or method can only be accessed within the class. Cannot be inherited!
(3)protected. This modifier indicates that the marked property or method can only be accessed within the class. Can be inherited
private The same class can be accessed, but neither subclasses nor external classes can access it;
protected The same class and subclasses can be accessed, but external classes cannot.
Access control of class members
Class members must be defined using the keywords public, protected or private
<?php 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; // 这行能被正常执行 echo $obj->protected; // 这行会产生一个致命错误 echo $obj->private; // 这行也会产生一个致命错误 $obj->printHello(); // 输出 Public、Protected 和 Private class MyClass2 extends MyClass { // 可以对 public 和 protected 进行重定义,但 private 而不能 protected $protected = 'Protected2'; function printHello() { echo $this->public; echo $this->protected; echo $this->private; } } $obj2 = new MyClass2(); echo $obj->public; // 这行能被正常执行 echo $obj2->private; // 未定义 private echo $obj2->protected; // 这行会产生一个致命错误 $obj2->printHello(); // 输出 Public、Protected2,但不会输出 Private class Bar { public function test() { $this->testPrivate(); $this->testPublic(); } public function testPublic() { echo "Bar::testPublic\n"; } private function testPrivate() { echo "Bar::testPrivate\n"; } } class Foo extends Bar { public function testPublic() { echo "Foo::testPublic\n"; } private function testPrivate() { echo "Foo::testPrivate\n"; } } $myFoo = new foo(); $myFoo->test(); // Bar::testPrivate // Foo::testPublic ?>
Access control of methods
Methods in a class must be defined using the keywords public, protected or private. If these keywords are not set, the method will be set to the default public.
<?php class MyClass { // 构造函数必须是 public public function construct() { } // 声明一个 public 的方法 public function MyPublic() { } // 声明一个 protected 的方法 protected function MyProtected() { } // 声明一个 private 的方法 private function MyPrivate() { } // 这个方法也是 public 的 function Foo() { $this->MyPublic(); $this->MyProtected(); $this->MyPrivate(); } } $myclass = new MyClass; $myclass->MyPublic(); // 这行能被正常执行 $myclass->MyProtected(); // 这行会产生一个致命错误 $myclass->MyPrivate(); // 这行会产生一个致命错误 $myclass->Foo(); // Public、Protected 和 Private 都被调用了 class MyClass2 extends MyClass { // This is public function Foo2() { $this->MyPublic(); $this->MyProtected(); $this->MyPrivate(); // 这行会产生一个致命错误 } } $myclass2 = new MyClass2; $myclass2->MyPublic(); // 这行能被正常执行 $myclass2->Foo2(); // Public 和 Protected 都被调用了,但 Private 不会被调用 ?>
The above is the detailed content of php: Access control for members in a class. For more information, please follow other related articles on the PHP Chinese website!