Php object-oriented basics-php Tutorial

Source: Internet
Author: User
Php object-oriented basics

  1. Class Person
  2. {
  3. // The following are the member attributes of a person.
  4. Var $ name; // name of a person
  5. Var $ ***; // gender of a person
  6. Var $ age; // age of a person
  7. // The following is the member method of a person.
  8. Function say () // The way this person can speak
  9. {
  10. Echo "this person is talking ";
  11. }
  12. Function run () // this person can walk
  13. {
  14. Echo "this person is walking ";
  15. }
  16. }
  17. ?>

3. 5. how to instantiate an object

As mentioned above, the unit of the php object-oriented program is the object, but the object is instantiated through the class. since our class will be declared, the next step is to instantiate the object. After the class is defined, we use the new keyword to generate an object. $ Object name = new class name (); object-> attribute $ p1-> name; $ p2-> age; $ p3-> ***; object-> method $ p1-> say (); $ p2-> run ();

5. 7. the special reference "$ this" is used. now we know how to access the members in the object, which is accessed through "object-> member, this is the form of accessing the members of the object outside the object. if I want to allow methods in the object to access the attributes of the object inside the object, or the methods in the object call other methods of this object. what should we do? Because all the members in the object must be called by objects, including calls between internal members of the object, a reference to the object $ this is provided in PHP, each object has an object reference $ this to represent this object and complete the call of its members. this is meant to be "this". in the above instance, we instantiate three instance objects $ P1, $ P2, and $ P3, each of which has a $ this representing the objects $ p1, $ p2, and $ p3 respectively.

  1. Class Person
  2. {
  3. // The following are the member attributes of a person.
  4. Var $ name; // name of a person
  5. Var $ ***; // gender of a person
  6. Var $ age; // age of a person
  7. // The following is the member method of a person.
  8. Function say () // The way this person can speak
  9. {Echo "My name is :". $ this-> name. "Gender :". $ this-> ***. "My age is :". $ this-> age."
  10. ";
  11. }

8. constructor and Destructor most classes have a special method called constructor. When an object is created, it automatically calls the constructor, that is, when the new keyword is used to instantiate the object, the constructor is automatically called.

The constructor declaration is the same as that of other operations, but its name must be _ construct (). This is a change in PHP5. in previous versions, the name of the constructor must be the same as the class name, which can still be used in PHP5, but it is rarely used now, in this way, the constructor can be made independent of the class name. when the class name changes, you do not need to change the corresponding constructor name. For backward compatibility, if a class does not have a method named _ construct (), PHP will search for a method written in php4 with the same name as the class name. Format: function _ construct ([parameter]) {...} only one constructor can be declared in a class. Instead, the constructor is called only once every time an object is created, so it is usually used to execute some useful initialization tasks. For example, assign an initial value to a property when creating an object.

  1. // Create a human
  2. Class Person
  3. {
  4. // The following are the member attributes of a person.
  5. Var $ name; // name of a person
  6. Var $ ***; // gender of a person
  7. Var $ age; // age of a person
  8. // Define a constructor parameter: name $ name, gender $ ***, and age $ age
  9. Function _ construct ($ name, $ ***, $ age)
  10. {
  11. // Assign the initial value to the member attribute $ this-> name through the $ name passed in by the constructor
  12. $ This-> name = $ name;
  13. // Assign the initial value to the member attribute $ this-> *** passed in through the constructor
  14. $ This-> *** = $ ***;
  15. // Assign the initial value to the member attribute $ this-> age through the $ age passed in by the constructor
  16. $ This-> age = $ age;
  17. }
  18. // How this person speaks
  19. Function say ()
  20. {
  21. Echo "My name is :". $ this-> name. "Gender :". $ this-> ***. "My age is :". $ this-> age."
  22. ";
  23. }
  24. }
  25. // Create three objects $ p1, p2, and $ p3 by using the constructor, and input three different real parameters: name, gender, and age.
  26. $ P1 = new Person ("zhang san", "male", 20 );
  27. $ P2 = new Person ("Li Si", "female", 30 );
  28. $ P3 = new Person ("Wang Wu", "male", 40 );
  29. // The following method of accessing the $ p1 object
  30. $ P1-> say ();
  31. // The following method of accessing the $ p2 object
  32. $ P2-> say ();
  33. // The following method is used to access the $ p3 object.
  34. $ P3-> say ();
  35. ?>

Output result: my name is Zhangsan Gender: Male and my age is: 20 my name is Li Si Gender: Female my age is: 30 my name is: wang Wu gender: my male age is: 40

The constructor is opposite to the constructor. The Destructor is newly added to PHP5 and does not contain the Destructor in PHP4. The Destructor allows you to perform some operations or complete some functions before destroying a class, such as closing a file and releasing a result set, the Destructor is executed when all references to an object are deleted or when the object is explicitly destroyed, that is, the number of destructor called before the object is destroyed in the memory. Similar to the constructor name, the destructor name of a class must be _ destruct (). The Destructor cannot contain any parameters. Format: function _ destruct (){......}

  1. // Create a human

  2. Class Person
  3. {
  4. // The following are the member attributes of a person.
  5. Var $ name; // name of a person
  6. Var $ ***; // gender of a person
  7. Var $ age; // age of a person
  8. // Define a constructor parameter: name $ name, gender $ ***, and age $ age

  9. Function _ construct ($ name, $ ***, $ age)

  10. {
  11. // Assign the initial value to the member attribute $ this-> name through the $ name passed in by the constructor
  12. $ This-> name = $ name;
  13. // Assign the initial value to the member attribute $ this-> *** passed in through the constructor
  14. $ This-> *** = $ ***;
  15. // Assign the initial value to the member attribute $ this-> age through the $ age passed in by the constructor
  16. $ This-> age = $ age;
  17. }
  18. // How this person speaks
  19. Function say ()
  20. {
  21. Echo "My name is :". $ this-> name. "Gender :". $ this-> ***. "My age is :". $ this-> age."
  22. ";
  23. }
  24. // This is a destructor called before the object is destroyed.
  25. Function _ destruct ()
  26. {
  27. Echo "goodbye". $ this-> name ."
  28. ";
  29. }

If an interface is provided to the external class, you can provide the setting and obtaining methods for private attributes outside the class to operate on private attributes. for example: prvate $ age; // The age of private attributes

  1. Function setAge ($ age) // provides a public age setting method for external users
  2. {
  3. If ($ age <0 | $ age> 130) // when assigning a value to an attribute, to avoid setting an invalid value to the attribute
  4. Return;
  5. $ This-> age = $ age;
  6. }
  7. Function getAge () // provides a public method for obtaining the age.
  8. {
  9. Return ($ this-> age );
  10. }

The above method is to set and obtain values for a member attribute. of course, you can also assign values and values for each attribute in the same way, complete access outside the class. JAVABEAN is the same !!!

10. _ set () _ get () _ isset () _ unset () application of four methods

In general, the class attribute is always defined as private, which is more in line with the actual logic. However, attributes are read and assigned frequently. Therefore, in PHP5, we predefine the two functions "_ get ()" and "_ set () to obtain and assign values to the attributes, and check the "_ isset ()" and the method "_ unset ()" for deleting the attributes ()". In the previous section, we set and obtain each attribute. in PHP5, we provided a method specifically for setting and obtaining values for the attribute, "_ set () "and" _ get () "methods. These two methods do not exist by default, but are manually added to the class, like the constructor (_ construct (), the class will only exist after it is added. you can add these two methods in the following way. of course, you can also add them in your own style:

  1. // _ Get () is used to obtain private attributes.

  2. Private function _ get ($ property_name)
  3. {

  4. If (isset ($ this-> $ property_name ))

  5. {
  6. Return ($ this-> $ property_name );
  7. } Else
  8. {
  9. Return (NULL );
  10. }
  11. }
  12. // _ Set () is used to set private attributes.
  13. Private function _ set ($ property_name, $ value)
  14. {
  15. $ This-> $ property_name = $ value;
  16. }

_ Get (): This method is used to obtain the attribute value of a private member. there is a parameter that is used to input the name of the member attribute you want to obtain and return the obtained attribute value, this method does not need to be manually called, because we can also make this method a private method, which is automatically called when the object directly obtains the private property. Because private attributes have been encapsulated, values cannot be obtained directly (for example, "echo $ p1-> name" is incorrect ), however, if you add this method to the class, when you use a statement such as "echo $ p1-> name" to directly obtain the value, it will automatically call _ get ($ property_name) to pass the attribute name to the parameter $ property_name. The value of the private attribute we passed in is returned through the internal execution of this method. If the member attributes are not encapsulated as private, the object itself will not automatically call this method. _ Set () method: This method is used to set values for private member attributes. There are two parameters. The first parameter is the attribute name for the set value, the second parameter is the value to be set for the attribute, with no return value. This method can also be made private without manual calls. it is automatically called when private property values are directly set. Similarly, private attributes are encapsulated, if the _ set () method is not available, for example, $ this-> name = 'hangsan', an error occurs, however, if you add the _ set ($ property_name, $ value) method to the class, it will be automatically called when the private property is assigned a value directly, pass the attribute such as name to $ property_name, and pass the value "zhangsan" to $ value. this method is used to assign values. If the member attributes are not encapsulated as private, the object itself will not automatically call this method. In order not to pass in invalid values, you can also make a judgment in this method.

  1. Class Person
  2. {
  3. // The following are the member attributes of a person, all of which are encapsulated private members.
  4. Private $ name; // name of a person
  5. Private $ ***; // gender of a person
  6. Private $ age; // age of a person
  7. // _ Get () is used to obtain private attributes.
  8. Private function _ get ($ property_name)
  9. {
  10. Echo "the _ get () method is automatically called when the private property value is directly obtained.
  11. ";
  12. If (isset ($ this-> $ property_name ))
  13. {
  14. Return ($ this-> $ property_name );
  15. }
  16. Else
  17. {
  18. Return (NULL );
  19. }
  20. }
  21. // _ Set () is used to set private attributes.
  22. Private function _ set ($ property_name, $ value)
  23. {
  24. Echo "when the private property value is directly set, the _ set () method is automatically called to assign values to the private property.
  25. ";
  26. $ This-> $ property_name = $ value;
  27. }
  28. }
  29. $ P1 = new Person ();
  30. // Operations that directly assign values to private properties automatically call the _ set () method to assign values.
  31. $ P1-> name = "James ";
  32. $ P1-> *** = "male ";
  33. $ P1-> age = 20;
  34. // Directly obtain the value of the private attribute. The _ get () method is automatically called to return the value of the member attribute.
  35. Echo "name:". $ p1-> name ."
  36. ";
  37. Echo "gender:". $ p1-> ***."
  38. ";
  39. Echo "age:". $ p1-> age ."
  40. ";
  41. ?>

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.