PHP classes and objects public properties and private properties _php tutorial

Source: Internet
Author: User
Tags control characters
This article gives you a simple example of the PHP class and object of public properties and private property usage, there is a need to understand the friend can refer to.


Private properties

A property that defines private properties can only be used in the class, which is called through $this->. An external reference to the private property will cause an error.

Instance:

The code is as follows Copy Code

Class people{
Private $name = "Li Ming";
}
$p =new people ();
Echo $p->name;
?>


Note: A field that has a private property set cannot be used in a subclass.

Public properties

In the operation of a PHP class, declaring a field uses public, private, protected, final, const, static to describe the scope of the object's data element, which is known as qualifying access control characters.

Properties that have a keyword public declaration are called Common Properties and are free to read and modify inside and outside of the class. This is obviously not safe enough to destroy the characteristics of class encapsulation.

If the field is not declared, the default is public.

Instance:

The code is as follows Copy Code

Class man{
Public $name = "John"; /* Set public properties */
var $age = 20;
}
$a =new man ();
echo $a->name. "
";
Echo $a->age;
?>

Changing property values

If the property is declared public, you can change the value of the property or assign an undefined property value as needed when you call it externally.

Instance:

The code is as follows Copy Code

Class man{
Public $name = "John";
var $age;
}
$a =new man ();
$a->name= "Tom"; /* Change attribute values */
$a->age=20; /* Give attribute value */
echo $a->name. "
";
Echo $a->age;
?>

http://www.bkjia.com/PHPjc/628817.html www.bkjia.com true http://www.bkjia.com/PHPjc/628817.html techarticle This article gives you a simple example of the PHP class and object of public properties and private property usage, there is a need to understand the friend can refer to. Private properties define the properties of private properties ...

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