1.创建一个简单的类
<?php class Example { public $item = 'hello zxl'; public $name; function Sample() { $this->Test(); } function Test() { echo 'ok' . "<br />"; echo $this->item; $regular = 100; echo $regular; } } $e = new Example(); $e->Sample(); ?>
<?php //Type Hinting class Test { public function __construct() { } public function Write() { echo 'I am writing form test ' . "<br />"; } } class Foo { public function __construct(Test $a) { $this->NewObj = $a; $this->NewObj->Write(); } } new Foo(new Test); ?>
pt1
<?php //Magic Methods class Test { public $name; public function Hello() { echo 'hello'; } function __get($param) { echo "$param does not exist"; } function __set($name, $value) { echo "you were going to set a property of $name - > $value"; $this->{$name} = $value; } function __call($param, $value) { echo "you were going to process $param($value)"; print_r($value); } } $test = new Test(); $test->Hello(); $test->nzzzame; //this is not in class goto "__get()" $test->age=10; //this is not in class goto "__set()" $test->Something('zzz','xxx','lll'); //this is not in class goto "__call()" ?>
<?php //Magic Methods class Test { function __construct() { echo 'item created' . "<br />"; } function __destruct() { echo 'item erased' . "<br />"; } function __toString() { return 'this is tostring' . "<br />"; } function __clone() { echo '1' . "<br />"; } } $test = new Test(); echo $test; $test2 = clone $test; ?>4.简单的文件读写类
<?php //Logger require_once('Log.class.php'); $log = new Log(); $log->Write('test.txt' , 'zzzzz'); //echo $log->Read('test.txt'); ?>
<?php //Logger class Log { private $_FileName; private $_Data; /** *@desc writes to a file *@param str $strFileName the name of the file *@param str $strData data to be appended to the file */ public function Write($strFileName , $strData) { $this->_FileName = $strFileName; $this->_Data = $strData; $this->_CheckPermission(); $this->_CheckData(); $handle = fopen($strFileName, 'a+'); fwrite($handle, $strData . "\r\n"); fclose($handle); } public function Read($strFileName) { $this->_FileName = $strFileName; $this->_CheckExists(); $handle = fopen($strFileName , 'r'); return file_get_contents($strFileName); } private function _CheckExists() { if(!file_exists($this->_FileName)) die('the file does not exist '); } private function _CheckPermission() { //if(!is_writable($this->_FileName)) //die('Change you chmod permissions to ' . $this->_FileName); } private function _CheckData() { if(strlen($this->_Data) < 1 ) die('null ' . $this->_Data); } } ?>
<?php //Decorator Pattern class Player { public $Data = array(); public function __construct(array $info) { $this->Data = $info; } } abstract class Player_Decorate { abstract public function Add($int); } class Player_Str_Decorate extends Player_Decorate { public function __construct(Player $p) { $this->Player = $p; //$this->Player->Data['str'] +=5 ; } public function Add($int) { $this->Player->Data['str'] += $int; } } class Player_Dex_Decorate extends Player_Decorate { public function __construct(Player $p) { $this->Player = $p; //$this->Player->Data['dex'] +=5 ; } public function Add($int) { $this->Player->Data['dex'] += $int; } } $P = new Player(array('str' => 10,'dex' => 15)); echo $P->Data['str']; echo '<br />'; echo $P->Data['dex']; echo '<hr />'; $Str = new Player_Str_Decorate($P); $Str->Add(25); echo $Str->Player->Data['str']; echo '<br />'; $Dex = new Player_Dex_Decorate($P); $Dex->Add(25); echo $Dex->Player->Data['dex']; ?>
<?php //Scop & Calculator class Calc { public $input; public $input2; private $output; function setInput($int) { $this->input = (int) $int; } function setInput2($int) { $this->input2 = (int) $int; } function calculate() { $this->output = $this->input * $this->input2; } function getResult() { return $this->output; } } $e = new Calc(); $e->setInput(5); $e->setInput2(55); $e->calculate(); echo $e->getResult(); ?>
chain_Methods.php
<?php //chain methods require('cupcake.php'); $cupcake = new Cupcake(); $cupcake->Nuts('10') ->Frosting('chocolate') ->Sprinkles('200'); print_r($cupcake->Cupcake); ?>
cupcake.php
<?php //chain methods class Cupcake { public $Cupcake = array(); public function Frosting($str) { $this->Cupcake['Frosting'] = $str; return $this; } public function Nuts($int) { $this->Cupcake['Nuts'] = (int) $int; return $this; } public function Sprinkles($int) { $this->Cupcake['Sprinkles'] = (int) $int; return $this; } } ?>