Share the differences between the three data types in the PHP5 class, PHP5 data types _php Tutorials

Source: Internet
Author: User

Share the difference between the three types of data in the PHP5 class, PHP5 data type


Public: Common Type
A method or property in a subclass that can invoke a public type through Self::var can call a method in the parent class through Parent::method
A method or property in an instance that can be $obj->var to invoke the public type

Protected: Protected Type
A method or property in a subclass that can call a protected type through Self::var can call a method 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 pairs of images 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

Attached code:

<?php/** * Parent can only call public or protected methods in the parent class, cannot call properties in the parent class * Self to invoke all data except for methods and properties of private types in the parent class */class the user{public $name; private $PA SSWD;  protected $email; Public Function __construct () {//print __class__. "  ";  $this->name= ' simple ';  $this->passwd= ' 123456 '; $this->email = ' bjbs_270@163.com ';  Public function Show () {print "Good";} Public Function Inuserclasspublic () {print __class__. ':: '. __function__. '  "; } protected function inuserclassprotected () {print __class__. ':: '. __function__. '  "; } Private Function Inuserclassprivate () {print __class__. ':: '. __function__. ' "; }} Class Simpleuser extends User {public Function __construct () {//print __class__.  "; Parent::__construct (); The Public Function show () {print $this->name.    Public "; Print $this->passwd. "  Private "; Print $this->email. "  protected ";} Public Function Insimpleuserclasspublic () {print __class__. ':: '. __function__. '  "; } protected function insimpleuserclassprotected () {print __class__. ':: '. __function__. '  "; } Private Function Insimpleuserclassprivate () {print __class__. ':: '. __function__. ' "; }} Class AdminUser extends Simpleuser {protected $admin _user; public function __construct () {//print __class__.  "; Parent::__construct (); } public Function Inadminuserclasspublic () {print __class__. ':: '. __function__. '  "; } protected function inadminuserclassprotected () {print __class__. ':: '. __function__. '  "; } Private Function Inadminuserclassprivate () {print __class__. ':: '. __function__. ' "; }} Class Administrator extends AdminUser {public Function __construct () {parent::__construct ();}} /** * Only public properties and methods in an instance of a class can be instantiated to invoke */$s = new Administrator ();p rint '-------------------'; $s->show (); >

http://www.bkjia.com/PHPjc/947911.html www.bkjia.com true http://www.bkjia.com/PHPjc/947911.html techarticle share the difference between the three data types in the PHP5 class, PHP5 data type Public: A method or property in a subclass that can be invoked by a self::var to invoke the type of publicly can pass the parent ...

  • Related Article

    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.