PHP5 the variable definition of classes in OOP follows an access control that is:
Public represents the global, and the inner and outer subclasses of the class can be accessed;
Private means that only this class can be used internally;
Protected is protected and is accessible only in this class or subclass or in the parent class;
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 in this class or subclass, you can also repeat the value in the subclass
Echo
;
Echo $this->private;//error because it's private, it's only possible to define her class BaseClass.
}
}
$obj 1 = new BaseClass ();
$obj 1->print_var ();
Echo $obj 1->protected;//error because it is protected, it can be called only in the inner or subclass parent class of this class.
echo $obj 1->private;//error the same as private, only within this class call
Echo $obj 1->public;
echo "
";
$obj 2 = new Subclass ();
Echo
;
Echo $obj 2->public;echo
;
Echo $obj 2->protected;
Echo $obj 2->private;//error
Echo $obj 2->protected;
?>
http://www.bkjia.com/PHPjc/486555.html www.bkjia.com true http://www.bkjia.com/PHPjc/486555.html techarticle the variable definition of a class in PHP5 OOP follows an access control, that is: public represents the global, the inner and outer subclasses of the class are accessible, and private means that only this class can use ...