This article mainly introduces a complete PHP class including seven kinds of syntax instructions. These syntaxes include attributes, static Attributes, methods, static methods, class constants, constructors, destructors, this article gives code examples and detailed annotations one by one to help you quickly understand how to write classes. Friends in need can refer to it
Seven syntax descriptions in the class
-Attributes
-Static attribute
-Method
-Static method
-Class constant
-Constructor
-Destructor
?
|
';
}
//Static method
public static function static_stuFunction() {
echo 'static_function',' '; } //Constructor is automatically called when creating an object public function __construct($stu_name) { $this->stu_name = $stu_name; echo '__construct',' '; } // Destructor is automatically called when destroying the object public function __destruct() { echo '__destruct',' '; } } // Instantiate class object $object = new Student('Tom'); //Object call attribute echo $object->stu_name,' '; //Object calls static properties echo $object::$stu_num,' '; //Class calls static attributes echo Student::$stu_num,' '; //Use objects to call methods and static methods respectively $object->stuFunction(); $object->static_stuFunction(); $object::stuFunction(); $object::static_stuFunction(); //Use classes to call methods and static methods respectively Student::stuFunction(); Student::static_stuFunction(); //Class calls class constants echo Student::STUDENT,' '; |
Summary:
Objects can call properties and static properties, while classes can only call static properties.
Objects can call methods and static methods, and classes can call methods and static methods.