Php 简明教程
PHP – Classes and Objects
类和对象的概念对于 PHP 的面向对象编程方法至关重要。 class 是其对象的模板描述。它包括处理属性的属性和函数。 object 是其类的实例。它的特点是类中定义的属性和函数。
The concept of classes and objects is central to PHP’s object-oriented programming methodology. A class is the template description of its objects. It includes the properties and functions that process the properties. An object is the instance of its class. It is characterized by the properties and functions defined in the class.

Defining a Class in PHP
PHP 有一个用于定义类的关键词 “ class ”。同样,PHP 提供了关键词 “ new ” 来声明任何给定类的对象。
To define a class, PHP has a keyword "class". Similarly, PHP provides the keyword "new" to declare an object of any given class.
在 PHP 中定义新类的通用格式如下 −
The general form for defining a new class in PHP is as follows −
<?php
class phpClass {
var $var1;
var $var2 = "constant string";
function myfunc ($arg1, $arg2) {
[..]
}
[..]
}
?>
关键词 class 后跟您想要定义的类的名称。类名称遵循与 PHP 变量相同的命名约定。后跟一对花括号,其中包含任意数量的变量声明(属性)和函数定义。
The keyword class is followed by the name of the class that you want to define. Class name follows the same naming conventions as used for a PHP variable. It is followed by a pair of braces enclosing any number of variable declarations (properties) and function definitions.
变量声明以另一个保留关键字 var 开始,后跟一个常规 $variable 名称;它们还可以对常量值进行初始赋值。
Variable declarations start with another reserved keyword var, which is followed by a conventional $variable name; they may also have an initial assignment to a constant value.
函数定义看起来很像独立的 PHP 函数,但它们是类中的本地函数,并且将用于设置和访问对象数据。类中的函数也称为方法。
Function definitions look much like standalone PHP functions but are local to the class and will be used to set and access object data. Functions inside a class are also called methods.
Example
这里有一个定义 Book 类型类的示例 −
Here is an example which defines a class of Book type −
class Book {
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($par){
$this->price = $par;
}
function getPrice(){
echo $this->price ."<br/>";
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
echo $this->title ." <br/>";
}
}
当从对象上下文中调用方法时,伪变量 $this 可用。 $this 指的是调用对象。
The pseudo-variable $this is available when a method is called from within an object context. $this refers to the calling object.
Book 类有两个 member variables (或属性): $title 和 $price 。成员变量(有时也称为实例变量)通常对每个对象具有不同的值;就像每本书的标题和价格都不同一样。
The Book class has two member variables (or properties) - $title and $price. The member variables (also sometimes called instance variables) usually have different values for each object; like each book has a title and price different from the other.
Book 类有函数(在类中定义的函数称为 methods )setTitle() 和 setPrice()。这些函数与对象和参数一起调用,用于分别设置标题和价格成员变量的值。
The Book class has functions (functions defined inside the class are called methods) setTitle() and setPrice(). These functions are called with reference to an object and a parameter, used to set the value of title and price member variables respectively.
Book 类还有 getTitle() 和 getPrice() 方法。调用时,它们会返回其引用已传递的对象的标题和价格。
The Book class also has getTitle() and getPrice() methods. When called, they return the title and price of the object whose reference is passed.
一旦定义了一个类,您就可以使用 new 运算符声明一个或多个对象。
Once a class is defined, you can declare one or more objects, using new operator.
$b1 = new Book;
$b2 = new Book;
new 运算符为每个对象的成员变量和方法分配所需的内存。在这里,我们创建了两个对象,这些对象彼此独立,并且它们将分别存在。
The new operator allocates the memory required for the member variables and methods of each object. Here we have created two objects and these objects are independent of each other and they will have their existence separately.
每个对象都可以通过 “ → ” 运算符访问其成员变量和方法。例如, b1 对象的 $title 属性为 “ $b1→title ”,要调用 setTitle() 方法,请使用 “ $b1→setTitle() ” 语句。
Each object has access to its member variables and methods with the "→" operator. For example, the $title property of b1 object is "$b1→title" and to call setTitle() method, use the "$b1→setTitle()" statement.
要设置 b1 对象的标题和价格,
To set the title and price of b1 object,
$b1->setTitle("PHP Programming");
$b1->setPrice(450);
同样,以下语句获取 b1 书的标题和价格 −
Similarly, the following statements fetch the title and price of b1 book −
echo $b1->getPrice();
echo $b1->getTitle();
Example
下面给出了一个完整的 PHP 脚本,该脚本定义 Book 类、声明两个对象和调用成员函数。
Given below is the complete PHP script that defines Book class, declares two objects and calls the member functions.
<?php
class Book {
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($par){
$this->price = $par;
}
function getPrice(){
echo $this->price ."\n";
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
echo $this->title ."\n";
}
}
$b1 = new Book;
$b2 =new Book;
$b1->setTitle("PHP Programming");
$b1->setPrice(450);
$b2->setTitle("PHP Fundamentals");
$b2->setPrice(275);
$b1->getTitle();
$b1->getPrice();
$b2->getTitle();
$b2->getPrice();
?>
它将生成以下 output −
It will produce the following output −
PHP Programming
450
PHP Fundamentals
275