Home Backend Development PHP Tutorial PHP类跟对象等代码说明

PHP类跟对象等代码说明

Jun 13, 2016 pm 12:28 PM
public sql

PHP类和对象等代码说明

1、定义和创建类和对象:

定义类要使用class关键字。例如:class 类名{//属性和方法}

创建对象使用new关键字。例如: $p1 = new 类名;,可以基于一个类创建多个对象。

 

2、 类属性值

(1) 在类中除了声明属性外,也可以为属性赋值,但是只能以如下几种形式给属性指定常量值:

1

示例1:    public $last_visitor = 'Donnan';    //正确    public $last_visitor = 9;    //正确    public $last_visitor = array('Jesse');    //正确    public $last_visitor = pick_visitor();    //错误    public $last_visitor = 'Chris'.'9';    //错误(2)在类中的方法为变量指定一个非常量值:    示例2:    class guest_book{        public $last_visitor;        public function update($comment, $visitor){            if(!empty($comments)){                array_unshift($this-&gt;comments, $comments);                $this-&gt;last_visitor = $visitor;    //在类中的方法为变量指定一个非常量值            }        }    }(3)在创建对象的过程中为变量指定非常量值,即在类的构造器中进行赋值。    构造器指的是在创建新对象时自动调用的一个方法,而其名字为__construct()    示例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-&gt;comments = $obj-&gt;comments;    //为变量指定非常量值                $this-&gt;last_visitor = $obj-&gt;last_visitor;    //为变量指定非常量值            }        }                }    $gb = new guest_book('stewart');    //为变量指定非常量值<br>//http://www.cnblogs.com/sosoft/<br>3、继承与覆盖<br>(1) 通过继承来扩展一个现存的类,使用关键字extends:

Copy after login

1

class 类名 extends 父类名{}(2)覆盖父类方法:    示例4:class DB{    function getResult(){        return $this-&gt;result;    }    function query($sql){        error_log("queryy() must bue overridden by a database-specific child");        return false;    }}class MySQL extends DB{    // //    MySQL类从父类DB继承了getResult()方法,但是重新实现了自己特定的MySQL方法query()    function query($sql){           $this-&gt;result = mysql_query($sql);    }}(3)在方法名前面前置parent::用以明确地调用一个父类的方法:    示例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)注意:当子类中的方法覆盖了父类中的方法时,除非明确地引用(parent::),否则不会自动调用父类的方法。

Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

What is the difference between HQL and SQL in Hibernate framework?

Usage of division operation in Oracle SQL Usage of division operation in Oracle SQL Mar 10, 2024 pm 03:06 PM

Usage of division operation in Oracle SQL

What does the identity attribute in SQL mean? What does the identity attribute in SQL mean? Feb 19, 2024 am 11:24 AM

What does the identity attribute in SQL mean?

Comparison and differences of SQL syntax between Oracle and DB2 Comparison and differences of SQL syntax between Oracle and DB2 Mar 11, 2024 pm 12:09 PM

Comparison and differences of SQL syntax between Oracle and DB2

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags

How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations? How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations? May 27, 2023 pm 01:10 PM

How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations?

How to solve the 5120 error in SQL How to solve the 5120 error in SQL Mar 06, 2024 pm 04:33 PM

How to solve the 5120 error in SQL

How to use SQL statements for data aggregation and statistics in MySQL? How to use SQL statements for data aggregation and statistics in MySQL? Dec 17, 2023 am 08:41 AM

How to use SQL statements for data aggregation and statistics in MySQL?

See all articles