This article will introduce to you the differences between public, private and protected in php5. All three of them are used in classes, but their attributes are completely different.
public: public properties or methods
You can call it through self::var or self::method in the subclass. You can call the method in the parent class through parent::method, but you cannot call the public attribute.
Can be called via $obj->var or self::method in an instance
protected: protected type
You can call it through self::var or self::method in the subclass, and you can call the method in the parent class through parent::method
Methods or properties of protected types cannot be called through $obj->var in an instance
private: private type
The attributes or methods of this type can only be used in this class. Private type attributes and methods cannot be called in instances of this class, subclasses, or instances of subclasses
2. The difference between self and parent
a). These two objects are commonly used in subclasses. The main difference between them is that self can call public or protected properties in the parent class, but parent cannot call
b).self:: It represents the static members (methods and properties) of the current class. Unlike $this, $this refers to the current object
Example
The code is as follows
代码如下 |
复制代码 |
class BaseClass {
public $public = 'public';
private $private = 'private';
protected $protected = 'protected';
function __construct(){
}
function print_var(){
print $this->public;echo ' ';
print $this->private; echo ' ';
print $this->protected; echo ' ';
}
}
class Subclass extends BaseClass {
// public $public = 'public2';
protected $protected ='protected2';
function __construct(){
echo $this->protected;//可以访问,因为类中定义为受保护的,所以在本类或子类中可以,子类中还可以重复付值
echo ' ';
echo $this->private;//error 因为是私有的只有在定义她的类baseclass中可以用
}
}
$obj1 = new BaseClass();
$obj1->print_var();
//echo $obj1->protected;//error 因为是受保护的,只有在本类内部或子类父类中可以调用
//echo $obj1->private;//error 同上私有的,只有在本类内调用
echo $obj1->public;
echo " ";
$obj2 = new Subclass();
echo ' ';
echo $obj2->public;echo ' ';
echo $obj2->protected;
//echo $obj2->private;//error
//echo $obj2->protected;//error
?>
|
|
Copy code
class BaseClass {
public $public = 'public';
| private $private = 'private';
protected $protected = 'protected'; |
function __construct(){
}
function print_var(){
Print $this->public;echo '
';
Print $this->private; echo '
';
Print $this->protected; echo '
';
}
}
class Subclass extends BaseClass {
// public $public = 'public2';
protected $protected ='protected2';
function __construct(){
echo $this->protected;//Can be accessed, because the class is defined as protected, so it can be used in this class or subclasses, and the value can be repeated in subclasses
echo '
';
}
}
$obj1 = new BaseClass();
$obj1->print_var();
//echo $obj1->protected;//error Because it is protected, it can only be called within this class or in a subclass or parent class
//echo $obj1->private;//error Same as above private, can only be called within this class
echo $obj1->public;
echo "
";
$obj2 = new Subclass();
echo '
';
echo $obj2->public;echo '
';
echo $obj2->protected;
//echo $obj2->private;//error
//echo $obj2->protected;//error
?>
Summary
public means global and can be accessed by subclasses inside and outside the class;
private means private and can only be used within this class;
protected means protected and can only be accessed in this class or subclass or parent class;
http://www.bkjia.com/PHPjc/632611.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632611.htmlTechArticleThis article will introduce to you the differences between public, private and protected in php5. All three of them are It is used in classes, but the properties are completely different. public: Public properties or methods in subclasses...