Access modifiers for PHP classes

Source: Internet
Author: User
Tags modifiers

Public, protected, private

Used to describe:

Member, which locations in the source code are accessible.

Public: In the class, outside the class, within the inheritance chain class

Protected: Within the class, within the inheritance chain class.

Private: Within this category.

One: Private: within this category.

<?php
Class H {
Public Function Hgetp () {
Echo $this->p;
echo self:: $sp;
// }
public static function Hgetp () {
Echo $this->p;
echo self:: $sp;
}
}
Class Z extends H {
protected $p = ' value ';
Private $p = ' value ';//$p is private and can only be called within this class
public static $sp = ' Static-value ';
Public Function Zgetp () {
echo self:: $sp;
// }
public static function Zgetp () {
Echo $this->p;
echo self:: $sp;
}
}
Class K extends Z {
Public Function Kgetp () {
Echo $this->p;
echo self:: $sp;
}
public static function Kgetp () {
Echo $this->p;
echo self:: $sp;
//}
}
Echo H::hgetp ();
Echo Z::zgetp ();
Echo K::kgetp ();
$o = new K;
$o-&GT;KGETP ();//output: notice:undefined property:k:: $p
echo "Var_dump ($o);//Output: Object (K) #1 (1) {["P": "Z":p rivate]=> string (5) "Value"}
$o->zgetp (); Output: Static-value

Two Protected: Within the class, within the inheritance chain class. (can be called within the inherited system of a class)

<?php
Class H {
Public Function Hgetp () {
Echo $this->p;
echo self:: $sp;
// }
public static function Hgetp () {
Echo $this->p;
echo self:: $sp;
}
}
Class Z extends H {
protected $p = ' value ';//$p is protected in this class, which is called inside the inheritance system of this class.
Private $p = ' value '; $p is private and can only be called inside this class
public static $sp = ' Static-value ';
Public Function Zgetp () {
echo self:: $sp;
// }
public static function Zgetp () {
Echo $this->p;
echo self:: $sp;
}
}
Class K extends Z {
Public Function Kgetp () {
Echo $this->p;
echo self:: $sp;
}
public static function Kgetp () {
Echo $this->p;
echo self:: $sp;
//}
}
Echo H::hgetp ();//If it is a static member, the parent class cannot access the static members defined by the child class.
Echo Z::zgetp ();
Echo K::kgetp ();
$o = new K;
$o->KGETP ();//output: Value
echo "Var_dump ($o);//Output: Object (K) #1 (1) {["P": "Z":p rivate]=> string (5) "Value"}
$o->zgetp (); Output: Static-value

PHP Inheritance:

Private members can be inherited.

If it is a static member, the parent class cannot access the static members defined by the child class.

Access modifiers for PHP classes

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.