PHP Object-Oriented basics

Source: Internet
Author: User
    1. Class Person
    2. {
    3. The following is the person's member property
    4. var $name; The name of the man
    5. var $***; Man's Sex
    6. var $age; The age of the person
    7. The following is a member method of the person
    8. function say ()//The way this person can speak
    9. {
    10. echo "This man is talking";
    11. }
    12. function run ()//The way this person can walk
    13. {
    14. echo "This man is walking";
    15. }
    16. }
    17. ?>
Copy Code

3.5. How to instantiate an object

As mentioned above, the unit of the object-oriented program of PHP is the object, but the object is also instantiated through the class, since our class will declare, the next step is to instantiate the object. When a class is defined, we use the New keyword to generate an object. $ Object name = new class name (), object, property $p 1->name; $p 2->age; $p 3->***; Object, Method $p 1->say (); $p 2->run ();

5.7. Use of special Reference "$this" Now we know how to access the members of the object, accessed through the "object-to-member" way, which is the form of accessing the members of the object outside of the object, so if I want to let the object's methods access the properties of this object inside the object, Or the method in the object to invoke other methods of this object what do we do then? Because all the members of the object are to be called with objects, including the calls between the internal members of the object, in PHP, I provide a reference to this object $this, each object has a reference to the object $this to represent the object, to complete the invocation of the object's internal members, This is meant to mean "this", in the above example, we instantiate three instance objects $p1, $P 2, $P 3, three objects within each of the existence of a $this respectively represents the object $p1, $p 2, $p 3.

    1. Class Person
    2. {
    3. The following is the person's member property
    4. var $name; The name of the man
    5. var $***; Man's Sex
    6. var $age; The age of the person
    7. The following is a member method of the person
    8. function say ()//The way this person can speak
    9. {echo "My name is called:". $this->name. "Gender:". $this->***. "My Age is:". $this->age. "
    10. ";
    11. }
Copy Code

8. Construction methods and destructor methods most classes have a special method called constructors. When an object is created, it automatically calls the constructor, which is automatically called when the object is instantiated using the New keyword.

The declaration of the constructor is the same as the declaration of the other operation, except that its name must be __construct (). This is a change in PHP5, in the previous version, the name of the constructor must be the same as the class name, which can still be used in PHP5, but is now used by very few people, the advantage is that the constructor is independent of the class name, when the class name changes, you do not need to change the corresponding constructor name.  To be backwards compatible, if a class does not have a method named __construct (), PHP will search for a constructor in PHP4 that has the same name as the class name. Format: function __construct ([parameter]) {...} Only one construction method can be declared in a class, but a constructor is called every time the object is created, and the method cannot be invoked actively, so it is often used to perform some useful initialization tasks. For example, an attribute is assigned an initial value when creating an object.

  1. Create a human
  2. Class Person
  3. {
  4. The following is the person's member property
  5. var $name; The name of the man
  6. var $***; Man's Sex
  7. var $age; The age of the person
  8. Define a constructor method parameter for name $name, gender $***, and age $age
  9. function __construct ($name, $***, $age)
  10. {
  11. The $name that is passed in by the constructor method assigns the $this->name value to the member property.
  12. $this->name= $name;
  13. The $*** that is passed in by the constructor method assigns the $this->*** value to the member property.
  14. $this->***=$***;
  15. The $age that is passed in by the constructor method assigns the $this->age value to the member property.
  16. $this->age= $age;
  17. }
  18. The way this guy talks.
  19. function Say ()
  20. {
  21. echo "My name is called:". $this->name. "Gender:" $this->***. "My Age is:". $this->age. "
  22. ";
  23. }
  24. }
  25. Create 3 Objects $p1, P2, $p 3 by constructing a method, passing in three different arguments for name, gender, and age, respectively
  26. $p 1=new person ("Zhang San", "male", 20);
  27. $p 2=new person ("John Doe", "female", 30);
  28. $p 3=new person ("Harry", "male", 40);
  29. The following accesses the speech method in the $p1 object
  30. $p 1->say ();
  31. The following accesses the speech method in the $p2 object
  32. $p 2->say ();
  33. The following accesses the speech method in the $p3 object
  34. $p 3->say ();
  35. ?>
Copy Code

The output is: My name is called: Zhang San Sex: Male my age is: 20 my name is called: John Doe Sex: Female my age is: 30 My name is called: Harry Sex: Male My age is: 40

A destructor is the opposite of a constructor function. Destructors are new additions to PHP5, and there are no destructors in PHP4. Destructors allow for some operations to be performed before destroying a class or to perform functions such as closing a file, releasing a result set, and so on, the destructor is removed from all references to an object or executed when the object is explicitly destroyed, that is, the object calls the destructor before it is destroyed in memory. Like the name of a constructor, a class's destructor name must be __destruct (). Destructors cannot have any parameters. Format: function __destruct () {...}

    1. Create a human

    2. Class Person
    3. {
    4. The following is the person's member property
    5. var $name; The name of the man
    6. var $***; Man's Sex
    7. var $age; The age of the person
    8. Define a constructor method parameter for name $name, gender $***, and age $age

    9. function __construct ($name, $***, $age)

    10. {
    11. The $name that is passed in by the constructor method assigns the $this->name value to the member property.
    12. $this->name= $name;
    13. The $*** that is passed in by the constructor method assigns the $this->*** value to the member property.
    14. $this->***=$***;
    15. The $age that is passed in by the constructor method assigns the $this->age value to the member property.
    16. $this->age= $age;
    17. }
    18. The way this guy talks.
    19. function Say ()
    20. {
    21. echo "My name is called:". $this->name. "Gender:" $this->***. "My Age is:". $this->age. "
    22. ";
    23. }
    24. This is a destructor that is called before the object is destroyed.
    25. function __destruct ()
    26. {
    27. echo "Good-bye" $this->name. "
    28. ”;
    29. }

Copy Code

If you provide an interface outside the class, you can manipulate the private property by providing a set method and a Get method outside the class for the private property. For example: Prvate $age; Private attribute Age

    1. function Setage ($age)//provide a method for the public to set the age of the outside
    2. {
    3. if ($age <0 | | $age >130)//When assigning a value to a property, in order to avoid an illegal value set to the property
    4. Return
    5. $this->age= $age;
    6. }
    7. function getage ()//provide a way for the outside public to obtain age
    8. {
    9. Return ($this->age);
    10. }
Copy Code

The method above is to set and get values for a member property, and of course you can use the same method for each property to assign and value the operation, complete the access work outside the class. JavaBean is the same!!!

Application of four methods of 10.__set () __get () __isset () __unset ()

In general, it is more realistic logic to always define the properties of a class as private. However, the read and assign operations for the properties are very frequent, so in PHP5, two functions "__get ()" and "__set ()" are predefined to get and assign their properties, as well as to check the Properties ' __isset () and delete the properties "__unset ()". In the previous section, we set and get a method for each property, and in PHP5 we were given a way to set the value and get the value specifically for the property, "__set ()" and "__get ()", two methods that were not by default, but that we manually added to the class, like the construction method ( __construct ()), the class is added in order to exist, you can add the two methods as follows, of course, can also be added according to personal style:

    1. The __get () method is used to get the private property

    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. The __set () method is used to set the private property
    13. Private Function __set ($property _name, $value)
    14. {
    15. $this $property _name = $value;
    16. }
Copy Code

__get () Method: This method is used to get the value of the private member property, there is a parameter, the parameter passed in the name of the member property you want to get, return the obtained property value, this method does not have to be called by us manually, because we can also make this method private method, The object is automatically called when the private property is obtained directly. Because the private property is already encapsulated, it is not possible to get the value directly (for example: "Echo $p 1->name"), but if you add this method to the class, use "Echo $p 1->name" When such a statement gets the value directly, it automatically calls the __get ($property _name) method, passes the property name to the parameter $property_name, and returns the value of our incoming private property through the internal execution of the method. If the member property is not encapsulated as private, the object itself does not automatically call this method. __set () Method: This method is used to set the value of the private member property, there are two parameters, the first parameter is you want to set the value of the property name, the second parameter is to set the value of the property, there is no return value. This method does not have to be called manually, it can also be made private, is directly set the value of the private property is automatically called, the same property private has been encapsulated, if there is no __set () This method is not allowed, such as: $this->name= ' Zhangsan ', this will make a mistake, but if you add __set ($property _name, $value) to the class, it is automatically called when you assign a value directly to the private property, and the attribute, such as name, is passed to $property_name. The value to be assigned "Zhangsan" to $value, through the execution of this method, to achieve the purpose of assignment. If the member property is not encapsulated as private, the object itself does not automatically call this method. In order not to pass in illegal values, you can also make a judgment in this method.

  1. Class Person
  2. {
  3. The following are the member properties of a person, both encapsulated private members
  4. Private $name; The name of the man
  5. Private $***; Man's Sex
  6. Private $age; The age of the person
  7. The __get () method is used to get the private property
  8. Private Function __get ($property _name)
  9. {
  10. echo "Automatically calls this __get () method when it directly acquires the value of a private property.
  11. ";
  12. if (Isset ($this-$property _name))
  13. {
  14. Return ($this, $property _name);
  15. }
  16. Else
  17. {
  18. return (NULL);
  19. }
  20. }
  21. The __set () method is used to set the private property
  22. Private Function __set ($property _name, $value)
  23. {
  24. echo "Automatically calls this __set () method to assign a value to a private property when setting the value of a private property directly
  25. ";
  26. $this $property _name = $value;
  27. }
  28. }
  29. $p 1=new person ();
  30. An operation that assigns a value directly to a private property, automatically calls the __set () method to assign the value
  31. $p 1->name= "Zhang San";
  32. $p 1->***= "Male";
  33. $p 1->age=20;
  34. Gets the value of the private property directly, automatically calls the __get () method, returns the value of the member property
  35. echo "Name:". $p 1->name. "
  36. ";
  37. echo "Gender:". $p 1->***. "
  38. ";
  39. echo "Age:" $p 1->age. "
  40. ";
  41. ?>
Copy Code
  • 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.