PHP 类 BaseClass

来源:互联网 发布:it外包公司 编辑:程序博客网 时间:2024/06/06 10:55

1.创建一个简单的类

<?phpclass 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();?>

2.类-类型引用

<?php//Type Hintingclass 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);?>

3.类中的各种内置方法

pt1

<?php//Magic Methodsclass 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()"?>

pt2

<?php//Magic Methodsclass 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.简单的文件读写类
Logger.php

<?php//Loggerrequire_once('Log.class.php');$log = new Log();$log->Write('test.txt' , 'zzzzz');//echo $log->Read('test.txt');?>

Log.class.php

<?php//Loggerclass 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);}}?>

5.数组参数的运算类

<?php//Decorator Patternclass 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'];?>

6.算数运算类

<?php//Scop & Calculatorclass 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();?>

7.php类中链的方法

chain_Methods.php

<?php//chain methodsrequire('cupcake.php');$cupcake = new Cupcake();$cupcake->Nuts('10')->Frosting('chocolate')->Sprinkles('200');print_r($cupcake->Cupcake);?>


cupcake.php

<?php//chain methodsclass 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;}}?>