private static $_instance; private function __construct() { } private static function get_instance() { if(!isset(self::$_instance)){ self::$_instance = new self(); }else{ return self::$_instance; } } //阻止clone private function __clone() { trigger_error('Clone is not allow', E_USER_ERROR); } function test() { echo "test"; } } $test = man::get_instance(); $test->test(); ?>
phpinfo(); exit; abstract class Human { public abstract function talk(); public abstract function eat(); public abstract function walk(); public function calcMath($a, $b) { echo $a * $b; } } class man extends Human { public function talk() { echo "Keep talking"; } public function eat() { echo "Keep eating"; } public function walk() { echo "Keep walking"; } } //$obj = new Character(); //Fatal error: Cannot instantiate abstract class $user = new man(); $user->talk(); $user->calcMath(5, 3);
{ $this->bars[] = new bar($where); } function getName() { return $this->name; } } class bar extends man { public $name; function bar($w) { $this->name = $w; } function getName() { return $this->name; } function whosdrunk() { $who = get_parent_class($this); if ($who == NULL) { return 'nobody'; } return eval("return " . $who . '::getName();'); } } $x = new man(); $x->getdrunk('The old Tavern'); var_dump($x->bars[0]->whosdrunk());
public $iq = 100; public function say() { $arr = array('早上好', '晚上好', 'shit'); if ($this->iq >= 100) { echo $arr[0]; } else { $rad = rand(0, 2); echo $arr[$rad]; } } } class car { public function hit($m) { $ra = rand(50, 150); $m->iq = $ra; } } $m = new man(); $c = new car(); echo $m->say(); echo "<br>"; $c->hit($m); echo $m->iq; echo "<br>"; echo $m->say(); echo "<br>"; echo $m->say(); echo "<br>";