PHP Object-oriented access control public,private,protected detailed _php tutorial

Source: Internet
Author: User
This article uses the example to explain the PHP5 object-oriented access control public,private,protected detailed explanation, the need friend can refer to.

The object-oriented mechanism is enhanced in PHP5, and the access control mechanism of public, private and protected is introduced. To understand from a literal meaning:

Public is undoubtedly common meaning, meaning that the class itself and its external subclasses can access this property or method;

Private English translation comes in the sense that the class itself can only be accessed within the class itself-the instantiated object handle cannot access the property and method, and the subclass cannot access it;

Protected a protected property or method that can be accessed only by the class itself or by the inside of the subclass, and the instantiated object handle cannot be accessed.

Now let's give an example to this question.

Suppose there is a gem museum manager Xiao Li, who divides the gems in the warehouse into three categories, Ruby, Sapphire, and Emerald. and divided the attributes, Ruby is the country of anyone can visit the label (public), Sapphire Manager Xiao Li family passed down (protected), Emerald is Xiao Li himself saw on the mountain (private).

So we can say that:

The public label of the Ruby, is the national-all, as long as the citizens of the legal State can see and take pictures.

Protected This is protected, only the small Li family (Xiao Li's children or grandchildren) in a particular room in order to visit and take pictures, not to get the room outside to see-too stingy!

Private this is privately-owned, Xiao Li does not want to let others know, but also do not want to take pictures, so only for Xiao Li in Xiao Li's room to see just, other people even if Xiao Li's children have no way to see


Instance

The code is as follows Copy Code

Error_reporting (E_all);

Class test{

Public $public;

Private $private;

protected $protected;

Static $instance;

Public Function __construct () {

$this->public = ' public
';

$this->private = ' private
';

$this->protected = ' protected
';

}

static function tank () {

if (!isset (self:: $instance [Get_class ()]))

{

$c = Get_class ();

Self:: $instance = new $c;

}

Return self:: $instance;

}

Public Function pub_function () {

echo "You request public function
";

Echo $this->public;

Echo $this->private; Private, internal can be called

Echo $this->protected; Protected, internal can be called

$this->pri_function (); Private method, which can be called internally

$this->pro_function (); The protected method, which can be called internally

}

protected function pro_function () {

echo "You request protected function
";

}

Private Function Pri_function () {

echo "You request private function
";

}

}

$test = Test::tank ();

Echo $test->public;

Echo $test->private; Fatal Error:cannot Access private property test:: $private

Echo $test->protected; Fatal error:cannot Access protected property test:: $protected

$test->pub_function ();

$test->pro_function (); Fatal Error:call to protected method test::p ro_function () from context

$test->pri_function (); Fatal Error:call to Private method test::p ri_function () from context

?>

public = ' Public '
'; $this->private = ' private
'; $this->protected = ' protected
'; } static function tank () {if (!isset (self:: $instance [Get_class ()])) {$c = Get_class (), self:: $instance = new $c;} return Self:: $instance; } public Function Pub_function () {echo ' request public function
"; Echo $this->public; Echo $this->private; Private, Echo $this->protected; can be called internally Protected, internal can call $this->pri_function (); Private method, internal can call $this->pro_function (); Protected method, internal can be called} protected function pro_function () {echo "You request protected function
"; } Private Function Pri_function () {echo "You request private function
"; } } $test = Test::tank (); Echo $test->public; Echo $test->private; Fatal Error:cannot Access private property test:: $private echo $test->protected; Fatal error:cannot Access protected property test:: $protected $test->pub_function (); $test->pro_function (); Fatal Error:call to protected method test::p ro_function () from Context $test->pri_function (); Fatal Error:call to Private method test::p ri_function () from context?>

From the above example, we can see that
Public: can be called inside class, you can instantiate the call.
Private: Can be called within the class, the instantiation of the call error.
Protected: Can call inside class, instantiation call error.

Class test{

Public $public;

Private $private;

protected $protected;

Static $instance;

Public Function __construct () {

$this->public = ' public
';

$this->private = ' private
';

$this->protected = ' protected
';

}

protected function tank () {//Private method cannot inherit, replace with public,protected

if (!isset (self:: $instance [Get_class ()]))

{

$c = Get_class ();

Self:: $instance = new $c;

}

Return self:: $instance;

}

Public Function pub_function () {

echo "You request public function
";

Echo $this->public;

}

protected function pro_function () {

echo "You request protected function
";

Echo $this->protected;

}

Private Function Pri_function () {

echo "You request private function
";

Echo $this->private;

}

}

Class Test1 extends test{

Public Function __construct () {

Parent::tank ();

Parent::__construct ();

}

Public Function tank () {

Echo $this->public;

Echo $this->private; notice:undefined property:test1:: $private

Echo $this->protected;

$this->pub_function ();

$this->pro_function ();

$this->pri_function (); Fatal Error:call to Private method test::p ri_function () from context ' test1 '

}

Public Function pro_extends_function () {

echo "You request extends_protected function
";

}

Public Function pri_extends_function () {

echo "You request Extends_private function
";

}

}

Error_reporting (E_all);

$test = new Test1 ();

$test-tank (); Subclasses and parent classes have properties and methods of the same name, and when instantiating subclasses, the properties and methods in the subclass cover the parent class.

?>

http://www.bkjia.com/PHPjc/629261.html www.bkjia.com true http://www.bkjia.com/PHPjc/629261.html techarticle This article uses the example to explain the PHP5 object-oriented access control public,private,protected detailed explanation, the need friend can refer to. The object-oriented mechanism is enhanced in PHP5, ...

  • Related Article

    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.