PHPClass class and object learning Notes

Source: Internet
Author: User
& Lt ;? Php *************************************** * *** Class object instance description (common Class) **************************************** * ** classClassDe
  1. /*************************************** ****
  2. Class object instance description (common Class)
  3. **************************************** ***/
  4. Class ClassDemo {
  5. Public $ PublicVar;
  6. Private $ PrivateVar; // private variables cannot be called outside
  7. Protected $ ProtectedVar; // The externally protected variable is not accessible to the subclass.
  8. Public static $ StaticVar = 0; // static variables static methods cannot access non-static methods and variables, non-static methods and variables can access static methods and variables
  9. Const constVar = ''; // when you have an attribute that you do not want to modify, consider using const as a constant, using class name: constant name; interface name: constant
  10. /*
  11. 1. an initial value must be assigned to a constant during definition.
  12. 2. constants cannot be modified.
  13. 3. the constant name cannot have $, which is generally in upper case. the default value is public.
  14. 4. constants use the self: constant name inside the class. class name: constant name
  15. */
  16. Public function _ construct (/* $ name */) {// this can contain a parameter. it must also contain a parameter when a parameter is created. Otherwise, an error may occur.
  17. // $ This-> PublicVar = $ name;
  18. // Self: $ StaticVar ++; // static variable internal access method external access method object name: variable name (ClassDemo: $ StaticVar)
  19. $ This-> PublicVar = '$ PublicVar ';
  20. $ This-> PrivateVar = '$ PrivateVar ';
  21. $ This-> ProtectedVar = '$ ProtectedVar ';
  22. Self: $ StaticVar ++;
  23. Echo "ClassDemo _ construct ";
  24. }
  25. Public function _ destruct () {// destructor
  26. }
  27. Final public function fun (){
  28. /*
  29. Final keyword (in PHP5)
  30. If you do not want the method to be overwritten by the inheritance time of other classes, you can use final
  31. When this keyword is used to modify a class, the class cannot be inherited by other classes (can be instantiated)
  32. Note: This keyword cannot be used to modify variables.
  33. */
  34. }
  35. } // End Class
  36. /*************************************** ****
  37. Class object instance description (abstract Class)
  38. **************************************** ***/
  39. Abstract class AbstractDemo {
  40. /***************************************
  41. 1. abstract classes cannot be instantiated.
  42. 2. abstract classes do not have to include abstract methods.
  43. That is to say, the abstract class can have no abstract method.
  44. 3. Once the abstract method is included, this class must
  45. Declare as abstract
  46. 4. abstract classes cannot have function bodies
  47. 5. if a class continues an abstract class, it must implement
  48. All abstract methods of this abstract class. (Unless it is declared as an abstract class)
  49. ***************************************/
  50. }
  51. /*************************************** ****
  52. Class object instance description (inherited Class)
  53. **************************************** ***/
  54. Class DemoTwo extends ClassDemo {
  55. }
  56. /*************************************** ****
  57. Class object instance description (interface)
  58. **************************************** ***/
  59. Interface Face {
  60. /*********************************
  61. 1. when a class implements an interface, it is required that the class must implement all methods of this interface.
  62. 2. methods of the interface cannot have a method body.
  63. 3. an interface cannot be instantiated.
  64. 4. the interface can have attributes, but must be a constant and public
  65. When to use the interface
  66. 1. set specifications
  67. 2. set standards for other programmers to implement, such:
  68. **********************************/
  69. Public function Name ();
  70. }
  71. Interface Face2 extends Face {
  72. /*******************************
  73. Inheritance interface
  74. The method for implementing the parent interface when inheriting interfaces
  75. ********************************/
  76. Const namevar = 20;
  77. }
  78. Class Demo implements Face2 {
  79. /**********************************
  80. Implementation interface. multiple interfaces can be implemented at the same time.
  81. When a class implements some interfaces, all interfaces and methods must be implemented.
  82. **********************************/
  83. Public $ Name1 = 0;
  84. Public function Name (){
  85. Echo Face2: namevar;
  86. }
  87. }
  88. ?>

Summary:Use self: (note not $ slef) to access static member attributes or methods within the class, for example:

Slef: $ country

Slef: myCountry ()

When the subclass accesses the static member attributes or methods of the parent class, use parent: (note not $ parent). The instance code is as follows:

Parent: $ country

Parent: myCountry ()

The attributes and methods of static members for external access are class name/subclass name: the instance code is as follows:

Person: $ country

Person: myCountry ()

Student: $ country

However, static methods can also be accessed through common objects.

• Class/object functions

• _ Autoload-try to load undefined classes

• Call_user_method_array-call a user method and pass the parameter array (obsolete)

• Call_user_method-call the user method for a specific object (obsolete)

• Class_alias-create an alias for a class

• Class_exists-check whether the class is defined

• Get_called_class-name of the Static Binding ("Late Static Binding") class

• Get_class_methods-returns an array composed of class method names

• Get_class_vars-returns an array composed of the default attributes of the class.

• Get_class-class name of the returned object

• Get_declared_classes-returns an array composed of the names of defined classes.

• Get_declared_interfaces-returns an array containing all declared interfaces.

• Get_declared_traits-returns an array of all defined traits

• Get_object_vars-returns an associated array composed of object attributes.

• Get_parent_class-return the parent class name of the object or class

• Interface_exists-check whether the interface has been defined

• Is_a-if the object belongs to this class or the class is the parent class of this object, TRUE is returned.

• Is_subclass_of-if this object is a subclass of this class, TRUE is returned.

• Method_exists-check whether the class method exists

• Property_exists-check whether the object or class has this attribute

• Trait_exists-check whether the specified trait exists

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.