1. Define and create classes and objects: class keywords must be used to define classes. For example, the class name {// attributes and Methods} creates an object using the new keyword. For example: $ p1new class name; you can create multiple objects based on a class. 2. Class property value (1) In addition to declaring an attribute in a class, you can also assign a value to the attribute, but it can only take the following forms
1. Define and create classes and objects: class keywords must be used to define classes. For example, the class name {// attributes and Methods} creates an object using the new keyword. For example: $ p1 = new class name; you can create multiple objects based on one class. 2. Class property value (1) In addition to declaring an attribute in a class, you can also assign a value to the attribute, but it can only take the following forms
1. Define and create classes and objects:
The class keyword must be used to define a class. Example: class name {// attributes and Methods}
Create an object using the new keyword. For example: $ p1 = new class name; you can create multiple objects based on one class.
2. Class property values
(1) In addition to declaring an attribute in a class, you can also assign values to the attribute, but you can only specify a constant value for the attribute in the following forms:
Example 1: public $ last_visitor = 'donnan '; // The correct public $ last_visitor = 9; // The correct public $ last_visitor = array ('jesse '); // correct public $ last_visitor = pick_visitor (); // error public $ last_visitor = 'chris '. '9'; // error (2) the method in the class specifies a scalar value for the variable: Example 2: class guest_book {public $ last_visitor; public function update ($ comment, $ visitor) {if (! Empty ($ comments) {array_unshift ($ this-> comments, $ comments); $ this-> last_visitor = $ visitor; // specify a very large value for the variable in the class method }}( 3) specify a very large value for the variable during object creation, that is, assign a value to the class constructor. The constructor is a method automatically called when a new object is created, and its name is _ construct () Example 3: class guset_book {public $ comments; public $ last_visitor; public function _ consturct ($ user) {$ dbh = mysqli_connect ('localhost', 'username', 'Password', 'SITE'); $ user = mysqli_real_escape_string ($ dbh, $ user); $ SQL = "select comments, last_visitor from guest_book where user = '$ user'"; $ r = mysqli_query ($ dbh, $ SQL ); if ($ obj = mysqli_fetch_object ($ dbh, $ r) {$ this-> comments = $ obj-> comments; // specify the extraordinary value for the variable $ this-> last_visitor = $ obj-> last_visitor; // specify the extraordinary value for the variable }}$ gb = new guest_book ('Stewart '); // specify the extraordinary value for the variable
// Http://www.cnblogs.com/sosoft/
3. Inheritance and coverage
(1) Extend an existing class through inheritance, using the keyword extends:
Class name extends parent class name {} (2) override parent class method: Example 4: class DB {function getResult () {return $ this-> result ;} function query ($ SQL) {error_log ("queryy () must bue overridden by a database-specific child"); return false ;}} class MySQL extends DB {// MySQL class inherits the getResult () method from the parent class DB, but implements its own specific MySQL method query () function query ($ SQL) {$ this-> result = mysql_query ($ SQL) ;}} (3) parent before the method name: method used to explicitly call a parent class: Example 5: fuction escape ( $ SQL) {$ safe_ SQL = mysql_real_escape_string ($ SQL); $ safe_ SQL = parent: escape ($ safe_ SQL); // parent method adds ''around $ dql; return $ safe_ SQL;} (4) Note: When the method in the subclass overwrites the method in the parent class, the method of the parent class is not automatically called unless it is explicitly referenced (parent.