PHP/Class/Class Definition
Содержание
- 1 A Basic PHP 4 Class
- 2 A Basic PHP 5 Class
- 3 A class is a collection of variables and functions working with these variables.
- 4 Aggregating an address object
- 5 Basic Object Accessing
- 6 Bird class
- 7 book class
- 8 Class Type Hints
- 9 Empty class
- 10 Implementing a Simple Class
- 11 Person class
- 12 PHP class declaration structure
- 13 Pre-defined methods
- 14 Using an aggregated class
A Basic PHP 4 Class
<source lang="html4strict">
<?php
class myPHP4Class { var $my_variable; function my_method($param) { echo "my_method($param)!\n"; echo "my variable is: "; echo "{$this->my_variable}\n"; } } $myinstance = new myPHP4Class(); $anotherinstance = new myPHP4Class();
?>
</source>
A Basic PHP 5 Class
<source lang="html4strict">
<?php
class myPHP5Class { public $my_variable; public function my_method($param) { echo "called my_method($param)!\n"; echo "my variable is: "; echo "{$this->my_variable}\n"; } }
?>
</source>
A class is a collection of variables and functions working with these variables.
<source lang="html4strict">
<?php class Cart {
var $items; function add_item ($artnr, $num) { $this->items[$artnr] += $num; } function remove_item ($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; return true; } else { return false; } }
} class Named_Cart extends Cart {
var $owner; function set_owner ($name) { $this->owner = $name; }
}
$ncart = new Named_Cart; $ncart->set_owner ("kris"); print $ncart->owner; $ncart->add_item ("10", 1); ?>
</source>
Aggregating an address object
<source lang="html4strict">
<? class Address {
protected $city; public function setCity($city) { $this->city = $city; } public function getCity() { return $this->city; }
} class Person {
protected $name; protected $address; public function __construct() { $this->address = new Address; } public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } public function __call($method, $arguments) { if (method_exists($this->address, $method)) { return call_user_func_array( array($this->address, $method), $arguments); } }
} ?>
</source>
Basic Object Accessing
<source lang="html4strict">
<?php
class myPHP4Class { var $my_variable; function my_method($param) { echo "my_method($param)!\n"; echo "my variable is: "; echo "{$this->my_variable}\n"; } } $myinstance = new myPHP4Class(); $anotherinstance = new myPHP4class(); $myinstance->my_variable = 10; $anotherinstance->my_variable = 20; $myinstance->my_method("MyParam");
?>
</source>
Bird class
<source lang="html4strict">
<?php class Bird {
function __construct($name, $breed){ $this->name = $name; $this->breed = $breed; }
} ?>
</source>
book class
<source lang="html4strict">
<?php
class book { private $title; private $isbn; private $copies; public function __construct($isbn) { $this->setIsbn($isbn); $this->getTitle(); $this->getNumberCopies(); } public function setIsbn($isbn) { $this->isbn = $isbn; } public function getTitle() { $this->title = "Python"; print "Title: ".$this->title."
"; } public function getNumberCopies() { $this->copies = "5"; print "Number copies available: ".$this->copies."
"; } } $book = new book("11111111X"); ?> </source>
Class Type Hints
<source lang="html4strict">
<?
class Dog { public function do_drool( ) { echo "Sluuuuurp\n"; } } class Cat { } function drool(Dog $some_dog) { $some_dog->do_drool( ); } $poppy = new Dog( ); drool($poppy); $poppy = new Cat( ); drool($poppy);
?>
</source>
Empty class
<source lang="html4strict">
<?php class StringThing {} $st = new StringThing(); print $st; ?>
</source>
Implementing a Simple Class
<source lang="html4strict">
<?php class SimpleClass {
public $data; public function echoMyData() { echo $this->data; }
} $sc_object = new SimpleClass(); $sc_object->data = "Hello, world! "; $another_object = new SimpleClass(); $another_object->data = "Goodbye, world! "; $sc_object->echoMyData(); $another_object->echoMyData(); ?>
</source>
Person class
<source lang="html4strict">
class Person {
public $name; protected $spouse; private $password; public function __construct($name) { $this->name = $name } public function getName() { return $name; } protected function setSpouse(Person $spouse) { if (!isset($this->spouse)) { $this->spouse = $spouse; } } private function setPassword($password) { $this->password = $password; }
}
</source>
PHP class declaration structure
<source lang="html4strict">
<? class Class_name {
var $attribute_1; var $attribute_N; function function1() { } function functionN() { }
} ?>
</source>
Pre-defined methods
<source lang="html4strict">
__construct() is called when a new instance of the class is created. __destroy() is called when an instance of the class passes out of memory (calling unset()). __autoload() is called when you refer to a class for the first time. __clone() is called when you copy the object using the clone keyword. __get() and __set() are called when you get or set an object property that is not defined. __get() takes a single parameter, the name of the property; __set() takes two parameters: the name of the property and the value. __call() is called when you try to call an undefined method. __call() takes two arguments: the method name that was used and an array containing any values that were passed to the method. __sleep() and __wakeup: __sleep() is called when you try to serialize() an object. __wakeup() is called when you unserialize() an object. __toString() is called when a string representation of the object is required.
</source>
Using an aggregated class
<source lang="html4strict">
<? class Address {
protected $city; protected $country; public function setCity($city) { $this->city = $city; } public function getCity() { return $this->city; } public function setCountry($country) { $this->country = $country; } public function getCountry() { return $this-> country;}
} class Person {
protected $name; protected $address; public function __construct() { $this->address = new Address; } public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } public function __call($method, $arguments) { if (method_exists($this->address, $method)) { return call_user_func_array( array($this->address, $method), $arguments); } }
} ?>
</source>