Tomm
27-10-2007, 08:44 PM
As asked for:
PHP5 Classes
Public, protected and private.
Public functions and variables are accessible from within the class, the class’ childs and externally. Protected functions and variables are accessible from within the class and the class’ childs but not externally. Private functions and variables are only accessible from within the class
Example:
<?php
class testone {
public $hiya;
protected $secret;
private $topsecret;
protected function myFunction() {
//do something
}
}
class testtwo extends testone {
private $msg;
public function getSecret() {
//Will work as secret is protected, making it accessible from its child
return $this->secret;
}
private function getMsg() {
return $this->msg;
}
function getTopSecret() {
//Won't work as topsecret is private.
return $this->topsecret;
}
function doSomething() {
//Will work as myFunction is protected and this class is a child of testone
return $this->myFunction();
}
}
$classone = new testone();
//Will work fine
echo $classone->hiya;
// Won't work as its protected
echo $classone->secret;
// Also won't work as its private
echo $classone->topsecret;
$classtwo = new testtwo();
//Will work fine. Function is public and since the class is a child of testone it can access secret.
echo $classtwo->getSecret();
//Won't work as getMsg is private
echo $classtwo->getMsg();
?>
__construct function
If a __construct function is defined in you class, it is automatically ran and passed the parameters you sent when you create the class.
__destruct function
The __destruct function, if defined, is called when the class is destroyed with unset or when it is no-longer used in the script. You can use this to do any clearing up or perhaps run a SQL query on the database to make any changes you need.
Examples:
<?php
class testone {
function __construct($name) {
echo "Hello " . $name . "!";
}
function __destruct() {
echo "Bye, bye!!";
}
}
$classone = new testone("Tom");
echo "lalala";
/*
Outputs:
Hello Tom!
lalala
Bye, bye!!
*/
?>
Static Functions and Variables
Static functions and variables are functions or variables that when inherited by a child class can be overwritten but also can still be access in their original form. Furthermore static classes are also accessible without creating a new instance of the class that they are a member of. They are accessible by using parent::functionName (When called from a child class) or classname:functionName where classname is the name of the class and functionName is the name of the static function.
Example:
<?php
class testone {
static function hiya($name) {
return "Hiya " . $name . "!!!";
}
}
class testtwo extends testone {
function sayHi($name) {
echo parent::hiya("Tom");
// Outputs: Hiya Tom!!!
}
function hiya($name) {
return "I am mean!!! Go away.";
}
function sayHiTwo($name) {
echo $this->hiya;
// Outputs: I am mean!!! Go away.
}
}
?>
Final functions and Variables
Final functions and variables are, as the name suggests, the final version of that function or variable and cannot be overwritten by a child class.
Example:
<?
class testone {
final function hiya($name) {
return "HAHAHA. Go away. This time you can't get rid of me!!";
}
}
class testtwo extends testone {
//Attempting to overwrite the function creates a error and halts the script.
function hiya($name) {
return "Hiya " . $name . "!!!";
}
}
?>
Interfaces
Interfaces are like templates for classes. While interfaces cannot be used as classes they can be implemented by a child class. If implemented the child class must follow the functions and/or variables outlined by the interface or a fatal error will occur.
Example:
<?php
interface theinterface {
public function Hiya();
private function Secret();
}
class goodclass implements theinterface {
public function Hiya() {
echo "hiya";
}
private function Secret() {
echo "psst.. secret";
}
}
class badclass implements theinterface {
//creates a fatal error because badFunction was not declaired in the interface
public function badFunction() {
echo "Muhahaha";
}
}
This is quite a simple tutorial and only outlines some of the power of classes in PHP5. If you are interested in a bit of reading I recommend PHP 5 Objects, Patterns, and Practice. You can buy it from: http://www.apress.com/book/view/1590593804
PHP5 Classes
Public, protected and private.
Public functions and variables are accessible from within the class, the class’ childs and externally. Protected functions and variables are accessible from within the class and the class’ childs but not externally. Private functions and variables are only accessible from within the class
Example:
<?php
class testone {
public $hiya;
protected $secret;
private $topsecret;
protected function myFunction() {
//do something
}
}
class testtwo extends testone {
private $msg;
public function getSecret() {
//Will work as secret is protected, making it accessible from its child
return $this->secret;
}
private function getMsg() {
return $this->msg;
}
function getTopSecret() {
//Won't work as topsecret is private.
return $this->topsecret;
}
function doSomething() {
//Will work as myFunction is protected and this class is a child of testone
return $this->myFunction();
}
}
$classone = new testone();
//Will work fine
echo $classone->hiya;
// Won't work as its protected
echo $classone->secret;
// Also won't work as its private
echo $classone->topsecret;
$classtwo = new testtwo();
//Will work fine. Function is public and since the class is a child of testone it can access secret.
echo $classtwo->getSecret();
//Won't work as getMsg is private
echo $classtwo->getMsg();
?>
__construct function
If a __construct function is defined in you class, it is automatically ran and passed the parameters you sent when you create the class.
__destruct function
The __destruct function, if defined, is called when the class is destroyed with unset or when it is no-longer used in the script. You can use this to do any clearing up or perhaps run a SQL query on the database to make any changes you need.
Examples:
<?php
class testone {
function __construct($name) {
echo "Hello " . $name . "!";
}
function __destruct() {
echo "Bye, bye!!";
}
}
$classone = new testone("Tom");
echo "lalala";
/*
Outputs:
Hello Tom!
lalala
Bye, bye!!
*/
?>
Static Functions and Variables
Static functions and variables are functions or variables that when inherited by a child class can be overwritten but also can still be access in their original form. Furthermore static classes are also accessible without creating a new instance of the class that they are a member of. They are accessible by using parent::functionName (When called from a child class) or classname:functionName where classname is the name of the class and functionName is the name of the static function.
Example:
<?php
class testone {
static function hiya($name) {
return "Hiya " . $name . "!!!";
}
}
class testtwo extends testone {
function sayHi($name) {
echo parent::hiya("Tom");
// Outputs: Hiya Tom!!!
}
function hiya($name) {
return "I am mean!!! Go away.";
}
function sayHiTwo($name) {
echo $this->hiya;
// Outputs: I am mean!!! Go away.
}
}
?>
Final functions and Variables
Final functions and variables are, as the name suggests, the final version of that function or variable and cannot be overwritten by a child class.
Example:
<?
class testone {
final function hiya($name) {
return "HAHAHA. Go away. This time you can't get rid of me!!";
}
}
class testtwo extends testone {
//Attempting to overwrite the function creates a error and halts the script.
function hiya($name) {
return "Hiya " . $name . "!!!";
}
}
?>
Interfaces
Interfaces are like templates for classes. While interfaces cannot be used as classes they can be implemented by a child class. If implemented the child class must follow the functions and/or variables outlined by the interface or a fatal error will occur.
Example:
<?php
interface theinterface {
public function Hiya();
private function Secret();
}
class goodclass implements theinterface {
public function Hiya() {
echo "hiya";
}
private function Secret() {
echo "psst.. secret";
}
}
class badclass implements theinterface {
//creates a fatal error because badFunction was not declaired in the interface
public function badFunction() {
echo "Muhahaha";
}
}
This is quite a simple tutorial and only outlines some of the power of classes in PHP5. If you are interested in a bit of reading I recommend PHP 5 Objects, Patterns, and Practice. You can buy it from: http://www.apress.com/book/view/1590593804