Code descriptions such as PHP classes and objects

Source: Internet
Author: User
Code descriptions for PHP classes and objects

1. Define and create classes and objects:

Define classes to use the class keyword. Example: Class Name {//attribute and method}

Create objects using the New keyword. For example: $p 1 = new class name, you can create multiple objects based on one class.

2. Class Property values

(1) In a class, in addition to declaring properties, you can assign a value to a property, but only specify a constant value for the property in the following ways:

Example 1:public $last _visitor = ' Donnan ';    Correct public $last _visitor = 9;    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 assigns a very measure to the variable: example 2:class guest_book{public $last _visitor; Public Function Update ($comment, $visitor) {if (!empty ($comments)) {Array_unshift ($this->comme                NTS, $comments);    $this->last_visitor = $visitor;    A method in a class assigns a very magnitude to a variable}}} (3) assigns a value to the variable in the process of creating the object, which is assigned in the constructor of the class.        A constructor refers to a method that is automatically called when a new object is created, with the name __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 a value for the variable $this->last_visitor = $obj->last_visitor;    Specify a value for the variable}}} $GB = new Guest_book (' Stewart '); Specify a value for a variable
http://www.cnblogs.com/sosoft/
3. Inheritance and coverage
(1) by inheriting to extend an existing class, use the keyword extends:
Class class name extends parent class name {} (2) Overrides 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 re-implements its own specific MySQL method query ()    function query ($ SQL) {           $this->result = mysql_query ($sql);}    } (3) Precede the method name with the parent:: A method to explicitly invoke a parent class:    example 5:fuction Escape ($sql) {    $safe _sql = mysql_real_escape_string ($sql) ;     $safe _sql = Parent::escape ($safe _sql);    The parent method adds ' around $DQL;    return $safe _sql;} (4) Note: When a method in a subclass overrides a method in the parent class, the parent class's method is not automatically called unless explicitly referenced (parent::).
  • 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.