PHP5 public,private,protected three different types of attributes _php tutorials

Source: Internet
Author: User
This article is to introduce the difference between the three of them in PhP5, the three of them are used in the class, but the properties are completely different.

Public: Common property or method

Can be called by Self::var or Self::method in a subclass, you can invoke a method in the parent class by Parent::method, but you cannot call the public property.

You can invoke the $obj->var or Self::method in the instance.

Protected: Protected Type

Can be called through Self::var or Self::method in a subclass, you can call methods in the parent class by Parent::method
Methods or properties of the protected type cannot be called through $obj->var in an instance

Private: Proprietary type

A property or method of that type can only be used in that class, and the properties and methods of private types cannot be called in instances, subclasses, or instances of subclasses of that class

The difference between 2.self and parent
A). These two objects are commonly used in subclasses. The main difference is that self can invoke a public or protected property in the parent class, but parent cannot call

b). Self:: It indicates that static members (methods and properties) of the current class are different from $this, $this refers to the current object


Cases

The code is as follows 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 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;//error

?>

Summarize

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;

http://www.bkjia.com/PHPjc/632611.html www.bkjia.com true http://www.bkjia.com/PHPjc/632611.html techarticle This article is to introduce the difference between the three of them in PhP5, the three of them are used in the class, but the properties are completely different. Public: Common Properties or methods in subclasses ...

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