This article mainly introduces a complete PHP class contains seven kinds of syntax description, the syntax includes attributes, static properties, methods, static methods, class constants, constructors, destructors, and this article gives you a quick understanding of how classes are written, and the friends you need can refer to
Seven syntax descriptions in a class
-Properties
-Static properties
-Method
-Static method
-Class constants
-Constructors
-destructor
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the |
; > <?php class Student {//Class properties, methods, and functions have access rights (functions and methods are the same concept)//private proprietary protected protected public publicly owned//class constants No access modifier CO NST STUDENT = ' Tom '; Property public $stu _name; static property public static $stu _num = 1; Method Public Function Stufunction () {echo ' non_static_function ', ' <br/> ';}//static method public static function static_s Tufunction () {echo ' static_function ', ' <br/> ';}//constructor automatically invokes public function __construct ($stu _name) {$this-& when creating objects Gt;stu_name = $stu _name; Echo ' __construct ', ' <br/> '; }//destructors automatically invoke public function __destruct () {echo ' __destruct ', ' <br/> ';}} when destroying objects //Instantiation class Object $object = new Student (' Tom '); Object Invocation Properties Echo $object->stu_name, ' <br/> '; Object invoke the Static property echo $object:: $stu _num, ' <br/> '; ClassInvoke Static properties Echo Student:: $stu _num, ' <br/> '; Use objects to invoke methods and static methods individually $object->stufunction (); $object->static_stufunction (); $object:: Stufunction (); $object:: Static_stufunction (); Use classes to invoke the method and static method Student::stufunction () respectively; Student::static_stufunction (); class to invoke class constants Echo Student::student, ' <br/> '; |
Summarize:
Objects can invoke properties and static properties, and classes can invoke only static properties.
objects can call methods and static methods, and classes can call methods and static methods.