Analysis of attributes and methods of the PHP5.0 object model _ PHP Tutorial

Source: Internet
Author: User
Analysis of attributes and methods of the PHP5.0 object model. Today we will introduce you to the topic->. If an object's attribute contains an object, you can use two-> operators to obtain the attributes of an internal object. Today we will introduce youYou can use the hyphen (-)-> operator to obtain the attributes of an internal object if an object's attributes contain an object. You can even use double-referenced strings to place these expressions. In the following example, the property room in the object House contains a group of Room objects.

The access method is similar to the access attribute. -> An operator is used to point to an instance. In the following example, getLastLogin is called. Methods are executed almost the same as functions outside the class.

If a class is inherited from another class, the attributes and methods in the parent class are valid in the subclass even if they are not declared in the subclass. As mentioned before, inheritance is very powerful. If you want to access an inherited attribute, you only need to reference it like accessing the attributes of the base class, using the: operator.

 
  1. Class Room
  2. {
  3. Public $ name;
  4. Function _ construct ($Name="Unnamed")
  5. {
  6. $ This->Name= $ Name;
  7. }
  8. }
  9. Class House
  10. {
  11. // Array (main product of the promotion product) of rooms
  12. Public $ room;
  13. }
  14. // Create empty house
  15. $Home=NewHouse;
  16. // Add some rooms
  17. $ Home-> room [] = new Room ("bedroom ");
  18. $ Home-> room [] = new Room ("kitchen ");
  19. $ Home-> room [] = new Room ("bathroom ");
  20. // Show the first room of the house
  21. Print ($ home-> room [0]-> name );
  22. ?>


The PHP5.0 object model has two special namespaces: The parent namespace points to the parent class, and the self namespace points to the current class. The following example shows how to use the parent namespace to call the constructor in the parent class. At the same time, we also use self to call another class method in the constructor.

 
  1. Class Animal // Animal
  2. {
  3. Public $ blood; // attribute of hot blood or cold blood
  4. Public $ name;
  5. Public function _ construct ($ blood, $Name=NULL)
  6. {
  7. $ This->Blood= $ Blood;
  8. If ($ name)
  9. {
  10. $ This->Name= $ Name;
  11. }
  12. }
  13. }
  14. Class Mammal extends Animal // Mammal
  15. {
  16. Public $ furColor; // fur color
  17. Public $ legs;
  18. Function _ construct ($ furColor, $ legs, $Name=NULL)
  19. {
  20. Parent: :__ construct ("warm", $ name );
  21. $ This->FurColor= $ FurColor;
  22. $ This->Legs= $ Legs;
  23. }
  24. }
  25. Class Dog extends Mammal
  26. {
  27. Function _ construct ($ furColor, $ name)
  28. {
  29. Parent: :__ construct ($ furColor, 4, $ name );
  30. Self: bark ();
  31. }
  32. Function bark ()
  33. {
  34. Print ("$ this-> name says 'Woof! '");
  35. }
  36. }
  37. $D=NewDog ("Black and Tan", "Angus ");
  38. ?>

The object is called as follows: if you need to determine the variable name at runtime, you can use an expression like $ this-> $ Property. If you want to call a method, you can use $ obj-> $ method ().

You can also use the-> operator to return the value of a function, which is not allowed in previous PHP versions. For example, you can write a PHP5.0 object model expression like this: $ obj-> getObject ()-> callMethod (). This avoids the use of an intermediate variable and helps implement some design patterns, such as The Factory mode.


Combine can be used together->. If an object's attribute contains an object, you can use two-> operators to obtain the attributes of an internal object. You...

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.