©
本文档使用
php.cn手册 发布
类是变量与作用于这些变量的函数的集合。变量通过var来定义,函数通过 function 来定义,而类则通过下面的语法定义:
<?php
class Cart {
var $items ; // 购物车中的物品
// 将 $num 个 $artnr 物品加入购物车
function add_item ( $artnr , $num ) {
$this -> items [ $artnr ] += $num ;
}
// 将 $num 个 $artnr 物品从购物车中取出
function remove_item ( $artnr , $num ) {
if ( $this -> items [ $artnr ] > $num ) {
$this -> items [ $artnr ] -= $num ;
return true ;
} elseif ( $this -> items [ $artnr ] == $num ) {
unset( $this -> items [ $artnr ]);
return true ;
} else {
return false ;
}
}
}
?>
上面的例子定义了一个 Cart 类,这个类由购物车中的商品构成的数组和两个用于从购物车中添加和删除商品的函数组成。
不能将一个类的定义分割到多个文件中。也不能将一个类的定义分割到多个 PHP 块中,除非该分割是在一个方法声明内部。以下用法将不起作用:
<?php
class test {
?>
<?php
function test () {
print 'OK' ;
}
}
?>
但是以下用法是可以的:
<?php
class test {
function test () {
?>
<?php
print 'OK' ;
}
}
?>
以下警告仅用于 PHP 4。
名称 stdClass 已经被 Zend 使用并保留。不能在 PHP 代码中定义名为 stdClass 的类。
函数名 __sleep 和 __wakeup 在 PHP 类中是魔术函数。除非想要与之联系的魔术功能,否则在任何类中都不能以此命名函数。
PHP 将所有以 __ 开头的函数名保留为魔术函数。除非想要使用一些见于文档中的魔术功能,否则建议不要在 PHP 中将函数名以 __ 开头。
在 PHP 4 中,var 变量的值只能初始化为常量。用非常量值初始化变量,需要一个初始化函数,该函数在对象被创建时自动被调用。这样一个函数被称之为构造函数(见下面)。
<?php
class Cart {
var $todays_date = date ( "Y-m-d" );
var $name = $firstname ;
var $owner = 'Fred ' . 'Jones' ;
var $items = array( "VCR" , "TV" );
}
class Cart {
var $todays_date ;
var $name ;
var $owner ;
var $items = array( "VCR" , "TV" );
function Cart () {
$this -> todays_date = date ( "Y-m-d" );
$this -> name = $GLOBALS [ 'firstname' ];
}
}
?>
类也是一种类型,就是说,它们是实际变量的蓝图。必须用 new 运算符来创建相应类型的变量。
<?php
$cart = new Cart ;
$cart -> add_item ( "10" , 1 );
$another_cart = new Cart ;
$another_cart -> add_item ( "0815" , 3 );
?>
上述代码创建了两个 Cart 类的对象 $cart 和 $another_cart ,对象 $cart 的方法 add_item() 被调用时,添加了 1 件 10 号商品。对于对象 $another_cart ,3 件 0815 号商品被添加到购物车中。
$cart 和 $another_cart 都有方法 add_item(),remove_item() 和一个 items 变量。它们都是明显的函数和变量。可以把它们当作文件系统中的某些类似目录的东西来考虑。在文件系统中,可以拥有两个不同的 README.TXT 文件,只要不在相同的目录中。正如从为了根目录访问每个文件需要输入该文件的完整的路径名一样,必须指定需要调用的函数的完整名称:在 PHP 术语中,根目录将是全局名字空间,路径名符号将是 ->。因而,名称 $cart->items 和 $another_cart->items 命名了两个不同的变量。注意变量名为 $cart->items ,不是 $cart->$items ,那是因为在 PHP 中一个变量名只有一个单独的美元符号。
<?php
// 正确,只有一个 $
$cart -> items = array( "10" => 1 );
// 不正确,因为 $cart->$items 变成了 $cart->""
$cart -> $items = array( "10" => 1 );
// 正确,但可能不是想要的结果:
// $cart->$myvar 变成了 $cart->items
$myvar = 'items' ;
$cart -> $myvar = array( "10" => 1 );
?>
在定义类的时候,无法得知将使什么名字的对象来访问:在编写 Cart 类时,并不知道之后对象的名称将会命名为 $cart 或者 $another_cart ,还是什么其它的名字。因而你不能在类中使用 $cart->items 。然而为了类定义的内部访问自身的函数和变量,可以使用伪变量 $this 来达到这个目的。 $this 变量可以理解为“我自己的”或者“当前对象”。因而 ' $this->items[$artnr] += $num ' 可以理解为“我自己的物品数组的 $artnr 计数器加 $num ”或者“在当前对象的物品数组的 $artnr 计数器加 $num ”。
Note:
伪变量 $this 通常未定义,如果其所在的方法是被静态调用的话。但这不是个严格规定:如果一个方法被从另一个对象内静态调用的话,则 $this 会被定义。此时 $this 的值是那个发出调用的对象。用下例演示:
<?php
class A
{
function foo ()
{
if (isset( $this )) {
echo '$this is defined (' ;
echo get_class ( $this );
echo ")\n" ;
} else {
echo "\$this is not defined.\n" ;
}
}
}
class B
{
function bar ()
{
A :: foo ();
}
}
$a = new A ();
$a -> foo ();
A :: foo ();
$b = new B ();
$b -> bar ();
B :: bar ();
?>以上例程会输出:
$this is defined (a) $this is not defined. $this is defined (b) $this is not defined.
Note:
有一些不错的函数用来处理类和对象。应该关注一下类/对象函数。
[#1] ender911t at gmail dot com [2015-09-21 21:19:58]
Overrides in child are called when calling a function from the parent
<?PHP
class Par
{
var $test;
function __construct($in)
{
$this->test = $in;
}
function getTest()
{
return $this->test;
}
function runTest()
{
return $this->getTest();
}
}
class Chi extends Par
{
var $i;
function __construct($in)
{
$this->i = $in;
parent::__construct($in."X");
}
function getTest()
{
return $this->i;
}
function runParentTestA()
{
return parent::runTest();
}
function runParentTestB()
{
return parent::getTest();
}
}
$p = new Par("par");
$c = new Chi("chi");
echo $p->runTest()." == par";
echo $c->runTest()." == chi";
echo $c->runParentTestA()." == chi";
echo $c->runParentTestB()." == chiX";
?>
[#2] anthonymccuen at yahoo dot com [2013-03-22 03:57:49]
You can also use "var" to declare multiple public instance variables like in C:
int main(int *argc, const char *argv[]) {
int a, b, c, d, e, f;
a = 50;
// blah blah blah code here
// ...
return 0;
}
Same as PHP:
<?php
class MyClass {
var $myvar, $anotherVar, $something, $str;
}
?>
[#3] masonswolf at gmail dot com [2012-05-16 23:38:50]
You can invoke a class above its definitions in some cases, but there are very important exceptions to this behavior. If your class extends another class, and the interpreter doesn't see the extended class first, then it won't be added to the symbol table until the code has stepped through it in runtime. As a result, if you try to invoke it above the definition, you'll get a "class not found" fatal error.
And those suck.
So, to provide an example, the following will output a fatal error
<?php
Bar::bar();
exit;
class Bar extends Foo { } //will fatal because Foo not seen yet
class Foo {
static function bar() { echo 'yep, this is Foo::bar'; }
}
?>
However, THIS code will work just fine:
<?php
Bar::bar();
exit;
class Foo {
static function bar() { echo 'yep, this is Foo::bar'; }
}
class Bar extends Foo { } //will work because Foo came first
?>
Notice that if you include a file containing the class you will extend, and then extend the class in the same file as its invocation, you can also get the class not found fatal. This happens even if the 'include' call happens before the child class's definition.
Eg. the following will also output a fatal error
<?php
include_once('file_with_foo.php');
Bar::bar();
exit;
class Bar extends Foo { }
?>
Hope that clarifies things.
[#4] pov at fingerprint.fr [2011-12-09 13:22:21]
I just discovered a behaviour that has to be mentioned.
Let's consider the following file
<?php
Foo::bar();
exit;
class Foo {
function bar() { echo 'yep, this is Foo::bar'; }
}
?>
You may expect :
- an error on the Foo::bar call, because the class is not defined
- removing the code after the exit without side-effect
But it won't, and it will output the string !
It seems every class definition is executed at parse-time.
But without errors; if your file is
<?php
Foo::bar();
exit;
class Foo {
function bar() { echo 'yep, this is Foo::bar'; }
}
class Foo {
function bar() { echo 'yep, this is another Foo::bar'; }
}
?>
it will still output the first string, not doing any "Already exiting class" error !
So if you intend preventing a double include by doing in an included file :
<?php
if (class_exists('Foo')) { return; }
class Foo {
static function register() { ... }
}
Foo::register();
?>
the Foo::register method won't be called !
Very strange development choice; I suppose it is due to ascending compatibility.
Hope it can help...
[#5] boukeversteegh at gmail dot com [2010-08-27 10:28:37]
You can also instantiate objects using variables containing the name of the class:
<?php
$type = 'Foo';
$foo = new $type();
print_r( $foo );
?>
The following works as well:
<?php
$somefoo = new Foo();
$anotherfoo = new $somefoo();
# so you don't have to do:
$anotherfoo = new get_class( $somefoo)();
?>