PHP类和对象等代码说明
Jun 06, 2016 pm 07:48 PM
php
code
create
definition
object
illustrate
1、定义和创建类和对象: 定义类要使用class关键字。例如:class 类名{//属性和方法} 创建对象使用new关键字。例如: $p1 = new 类名;,可以基于一个类创建多个对象。 2、 类属性值 (1) 在类中除了声明属性外,也可以为属性赋值,但是只能以如下几种形
1、定义和创建类和对象:
定义类要使用class关键字。例如:class 类名{//属性和方法}
创建对象使用new关键字。例如: $p1 = new 类名;,可以基于一个类创建多个对象。
2、 类属性值
(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->comments, $comments); $this->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->comments = $obj->comments; //为变量指定非常量值 $this->last_visitor = $obj->last_visitor; //为变量指定非常量值 } } } $gb = new guest_book('stewart'); //为变量指定非常量值<br>//http://www.cnblogs.com/sosoft/<br>3、继承与覆盖<br>(1) 通过继承来扩展一个现存的类,使用关键字extends:
Copy after login
class 类名 extends 父类名{} (2)覆盖父类方法: 示例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类从父类DB继承了getResult()方法,但是重新实现了自己特定的MySQL方法query() function query($sql){ $this->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
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
