public,private,protected Differences in PHP class and example Analysis _php tutorials

Source: Internet
Author: User
One, the difference between the public,private,protected
Public: Permissions are maximum, can be called internally, instance calls, and so on.
Protected: Protected type, used for this class and for inheriting class calls.
Private: Proprietary type, only used in this class.

Two, example
Copy CodeThe code is as follows:
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
?>

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.
Copy CodeThe code is as follows:
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.
?>

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

http://www.bkjia.com/PHPjc/327634.html www.bkjia.com true http://www.bkjia.com/PHPjc/327634.html techarticle One, public,private,protected the difference public: permissions are the largest, can be called internally, instance calls and so on. Protected: Protected type, used for this class and for inheriting class calls. Private: Private ...

  • 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.