The difference of public,private,protected in PHP class and the _php technique of case analysis

Source: Internet
Author: User
Tags php class
One, the difference of public,private,protected
Public: Permissions are the largest, can be called internally, instance calls, and so on.
Protected: Protected type, used for this class and inheritance class invocation.
Private: Proprietary type, only used in this class.

Two, example
Copy Code code as follows:

<?php
Error_reporting (E_all);
Class test{
Public $public;
Private $private;
protected $protected;
Static $instance;
Public Function __construct () {
$this->public = ' public <br> ';
$this->private = ' private <br> ';
$this->protected = ' protected <br> ';
}
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 are request public function<br>";
Echo $this->public;
Echo $this->private; Private, internal can call
Echo $this->protected; Protected, internal can call
$this->pri_function (); Private method, which can be called internally
$this->pro_function (); Protected method, internal can call
}
protected function pro_function () {
echo "You request protected function<br>";
}
Private Function Pri_function () {
echo "You request private function<br>";
}
}
$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 the context
$test->pri_function (); Fatal Error:call to Private method test::p the Ri_function () from the context
?>

From the example above, we can see that
Public : Can be called inside class, you can instantiate the call.
Private: You can call the class inside, instantiate the call error.
Protected: Can call inside class, instantiate call error.
Copy Code code as follows:

<?php
Class test{
Public $public;
Private $private;
protected $protected;
Static $instance;
Public Function __construct () {
$this->public = ' public <br> ';
$this->private = ' private <br> ';
$this->protected = ' protected <br> ';
}
protected function tank () {//Private method cannot be inherited, 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 are request public function<br>";
Echo $this->public;
}
protected function pro_function () {
echo "You request protected function<br>";
Echo $this->protected;
}
Private Function Pri_function () {
echo "You request private function<br>";
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 the Ri_function () from the context ' test1 '
}
Public Function pro_extends_function () {
echo "You request extends_protected function<br>";
}
Public Function pri_extends_function () {
echo "You request Extends_private function<br>";
}
}
Error_reporting (E_all);
$test = new Test1 ();
$test-> tank (); Properties and methods with the same name as the subclass and the parent class, when instantiating subclasses, the properties and methods in the subclass will cover the parent class.
?>

From the example above, we can see that
Public in the public:test can be inherited.
Private in the private:test can not be inherited.
The protected in the protected:test can be inherited.
The static in the static:test can be inherited.
Alas, for these things, always do not like to remember, with the time, always feel wrong, but also to check, so write an example, to facilitate their own view.

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.