Access control in the PHP5! Public, private,protected_php Tutorial

Source: Internet
Author: User

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

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