Public and Private attributes of php classes and objects

Source: Internet
Author: User
This article provides a simple example to illustrate the usage of public and private attributes of php classes and objects. if you need to know, you can refer to private attributes to define private attributes) only

This article provides a simple example to illustrate the usage of public and private attributes of php classes and objects. For more information, see.

Private attributes

Attributes that define private attributes can only be used in this class. in this class, you can use $ this-> to call these attributes. if you reference private attributes externally, an error is returned.

The instance code is as follows:

  1. Class People {
  2. Private $ name = "li ming ";
  3. }
  4. $ P = new People ();
  5. Echo $ p-> name;
  6. ?>

Note: fields with private attributes cannot be used in subclass.

Public attributes

In php operations, when declaring a field, public, private, protected, final, const, and static are used to describe the scope of the data element of the object. such characters are called restricted access control characters.

Attributes with the keyword public declaration are called public attributes, which can be freely read and modified inside and outside the class. this is obviously not safe enough and breaks the class encapsulation characteristics.

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

The instance code is as follows:

  1. Class Man {
  2. Public $ name = "John";/* set public attributes */
  3. Var $ age = 20;
  4. }
  5. $ A = new Man ();
  6. Echo $ a-> name ."
    ";
  7. Echo $ a-> age;
  8. ?>

Change attribute value

If the attribute is declared as public, you can change the attribute value or assign the undefined attribute value as needed during external calls.

The instance code is as follows:

  1. Class Man {
  2. Public $ name = "John ";
  3. Var $ age;
  4. }
  5. $ A = new Man ();
  6. $ A-> name = "Tom";/* Change the attribute value */
  7. $ A-> age = 20;/* assign attribute values */
  8. Echo $ a-> name ."
    ";
  9. Echo $ a-> age;
  10. ?>

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.