class
A class is a collection of variables and functions working with these variables. Variables are defined by var and functions by function. A class is defined using the following syntax:
<?php
class Cart {
var $items; // Items in our shopping cart
// Add $num articles of $artnr to the cart
function add_item($artnr, $num) {
$this->items[$artnr] += $num;
}
// Take $num articles of $artnr out of the cart
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;
}
}
}
?>
This defines a class named Cart that consists of an associative array of articles in the cart and two functions to add and remove items from this cart.
You can NOT break up a class definition into multiple files. You also can NOT break a class definition into multiple PHP blocks, unless the break is within a method declaration. The following will not work:
<?php
class test {
?>
<?php
function test() {
print 'OK';
}
}
?>
However, the following is allowed:
<?php
class test {
function test() {
?>
<?php
print 'OK';
}
}
?>
The following cautionary notes are valid for PHP 4.
The name stdClass is used internally by Zend and is reserved. You cannot have a class named stdClass in PHP.
The function names __sleep and __wakeup are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them. See below for more information.
PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.
In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below).
<?php
class Cart {
/* None of these will work in PHP 4. */
var $todays_date = date("Y-m-d");
var $name = $firstname;
var $owner = 'Fred ' . 'Jones';
/* Arrays containing constant values will, though. */
var $items = array("VCR", "TV");
}
/* This is how it should be done. */
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'];
/* etc. . . */
}
}
?>
Classes are types, that is, they are blueprints for actual variables. You have to create a variable of the desired type with the new operator.
<?php
$cart = new Cart;
$cart->add_item("10", 1);
$another_cart = new Cart;
$another_cart->add_item("0815", 3);
?>
This creates the objects $cart and $another_cart, both of the class Cart. The function add_item() of the $cart object is being called to add 1 item of article number 10 to the $cart. 3 items of article number 0815 are being added to $another_cart.
Both, $cart and $another_cart, have functions add_item(), remove_item() and a variable items. These are distinct functions and variables. You can think of the objects as something similar to directories in a filesystem. In a filesystem you can have two different files README.TXT, as long as they are in different directories. Just like with directories where you'll have to type the full pathname in order to reach each file from the toplevel directory, you have to specify the complete name of the function you want to call: in PHP terms, the toplevel directory would be the global namespace, and the pathname separator would be ->. Thus, the names $cart->items and $another_cart->items name two different variables. Note that the variable is named $cart->items, not $cart->$items, that is, a variable name in PHP has only a single dollar sign ($).
<?php
// correct, single $
$cart->items = array("10" => 1);
// invalid, because $cart->$items becomes $cart->""
$cart->$items = array("10" => 1);
// correct, but may or may not be what was intended:
// $cart->$myvar becomes $cart->items
$myvar = 'items';
$cart->$myvar = array("10" => 1);
?>
Within a class definition, you do not know under which name the object will be accessible in your program: at the time the Cart class was written, it was unknown whether the object would be named $cart, $another_cart, or something else later. Thus, you cannot write $cart->items within the Cart class itself. Instead, in order to be able to access its own functions and variables from within a class, one can use the pseudo-variable $this which can be read as 'my own' or 'current object'. Thus, '$this->items[$artnr] += $num' can be read as 'add $num to the $artnr counter of my own items array' or 'add $num to the $artnr counter of the items array within the current object'.
Замечание:
The $this pseudo-variable is not usually defined if the method in which it is hosted is called statically. This is not, however, a strict rule: $this is defined if a method is called statically from within another object. In this case, the value of $this is that of the calling object. This is illustrated in the following example:
<?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.
Замечание:
There are some nice functions to handle classes and objects. You might want to take a look at the Class/Object Functions.
Коментарии
You can also instantiate objects using variables containing the name of the class:
<?php
$type = 'Foo';
$foo = new $type();
print_r( $foo );
/* Prints:
Foo Object
(
)
*/
?>
The following works as well:
<?php
$somefoo = new Foo();
$anotherfoo = new $somefoo();
# so you don't have to do:
$anotherfoo = new get_class( $somefoo)();
?>
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...
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.
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;
}
?>
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";
?>