Attributes and class methods in PHP5 class

Source: Internet
Author: User
This article describes in detail the attributes and class methods in the PHP5 class. if you need to learn, you can refer to the message. Prevent unauthorized access to encapsulated data. Users can only access data through a pre-defined method, and can simply add the control logic

This article describes in detail the attributes and class methods in the PHP5 class. For more information, see.

PHP5 class method

Process: A process is a sequence of statements defined during programming. it is used to complete a specified operation.

Function: a function has a returned value, which is also a sequence of defined statements.

Method: In the object-oriented concept, a sequence of statements in the class.

In general, functions and methods are common in object-oriented concepts.

Read attributes using methods

In the following example, the attribute is set to private and the public getName () method is declared to obtain the value of the attribute $ name and call getName () the method returns the value of $ name through return $ this-> name.

The instance code is as follows:

  1. Class Person
  2. {
  3. Private $ name = "NoName"; // private member $ name
  4. Public function getName (){
  5. Return $ this-> name;
  6. }
  7. }
  8. $ Newperson = new Person ();
  9. Echo ". $ newperson-> getName ();
  10. ?>

Note: $ this-> name is used to obtain the local attribute when the method calls the local attribute. in this example, the public getName () method is set, that is, the user can only get $ name, but cannot change its value. this is the benefit of encapsulation.

Encapsulation refers to bundling object state information (attributes) and behavior (methods) into a logical unit. PHP5 encapsulates and declares data as private, and then provides one or more public (public) methods to perform operations on this attribute:

This prevents unauthorized access to encapsulated data. Users can only access data by using predefined methods, and can add control logic to restrict unreasonable operations on attributes;

It is conducive to ensuring data integrity;

Ease of modification and enhanced code maintainability;

Method parameters

You can pass the variable to the method through the parameter when the method is defined. the following 5th rows define the method parameter $ _. when using this method, you can pass parameter variables to the method. the variable accepted by the method is a local variable, which is valid only within the method. you can pass the variable value to the attribute to apply the variable to the entire object.

The instance code is as follows:

  1. Class Person
  2. {
  3. Private $;
  4. Function setA ($ _ ){
  5. $ Thia-> a =$ _;
  6. }
  7. Function getA (){
  8. Return $ this->;
  9. }
  10. }
  11. $ Newperson = new Person ();
  12. $ Newperson-> setA (100 );
  13. Echo $ newperson-> getA ();
  14. ?>

If the declared method has parameters, but no parameters are passed when the method is called, or the number of parameters is insufficient, the system reports an error. if the number of parameters exceeds the number of method-defined parameters, PHP ignores more parameters and does not report an error. you can set the default value for the parameter during function definition. when a method is called, if no parameter is passed, the parameter variable is filled with the default value.

The instance code is as follows:

  1. Class
  2. {
  3. Public $ name = "tom ";
  4. }
  5. Class Person
  6. {
  7. Private $;
  8. Function setA ($ _ ){
  9. $ This-> a =$ _;
  10. }
  11. Function getA (){
  12. Return $ this->;
  13. }
  14. }
  15. $ A1 = new ();
  16. $ P = new Person ();
  17. $ P-> setA ($ a1 );
  18. Echo $ p-> getA ()-> name;
  19. ?>

Use of attributes: Call the attributes of an object through the-> symbol of the referenced variable. call the attributes of the same object through the $ this-> symbol in the method.

The instance code is as follows:

  1. Class Person
  2. {
  3. Public $ name = "NoName"; // defines the public attribute $ name
  4. Public $ age = 20; // defines the public attribute $ age
  5. }
  6. $ P = new Person (); // create an object
  7. Echo ". $ p-> name; // attribute $ name of the output object $ p
  8. Echo" ";
  9. Echo ". $ p-> age; // Output $ age attributes
  10. ?>

In the PHP5 class, we can also change the attribute value. of course, we must note that the attribute value is modified through public. let's modify this example:

The instance code is as follows:

  1. Class Person
  2. {
  3. Public $ name = "NoName"; // public variable $ name
  4. Public $ age = 20; // public variable $ age
  5. }
  6. $ P = new Person ();
  7. $ P-> name = "Tom"; // I am Tom
  8. $ P-> age = 25; // age 25
  9. Echo ". $ p-> name; // Output name
  10. Echo" ";
  11. Echo ". $ p-> age; // age
  12. ?>

Create a Person object and change its attributes. name it and view its name. you are the God of the Person object in the machine. according to your defined rules, the Person object in the real memory is created and has the attributes that can be changed.

Initial values of attributes

In PHP5, you can choose not to set the initial value in the attribute definition, or assign the following red type of initial value. PHP to eight simple types:

Four scalar types:

Boolean)

Integer)

Float ")

String)

Two composite types:

Array)

Object)

The last is a special type:

Resource)

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.