/** * This function compares all the human ages and returns them in a sorted array * * @access public * @author rasmus <*****@*****.**> * @param Human $a first compared human * @param Human $b second compared human * @return int return <samp>0</samp> if <code>$a</code> and <code>$b</code> are equal, <samp>-1</samp> or <samp>1</samp> if <code>$a</code> is smaller than <code>$b</code> */ public static function compare($a, $b) { if ($a->getAge() == $b->getAge()) { return 0; } return $a->getAge() < $b->getAge() ? -1 : 1; }
/** * @throws \ValidationException * @throws null */ public function testCallback_SetOnAfterExistsCallback_CallbackCalled() { $dog1 = new Dog(); $dog1->Name = 'Jim bob'; $dog2 = new Dog(); $dog2->Name = 'Super Dog'; $owner = new Human(); $owner->Name = 'Hilly Stewart'; $owner->write(); $owner->onAfterExistsCallback(function ($owner) use($dog1) { $dog1->OwnerID = $owner->ID; $dog1->write(); }); $owner->write(); $owner->onAfterExistsCallback(function ($owner) use($dog2) { $dog2->OwnerID = $owner->ID; $dog2->write(); }); $this->assertTrue($owner->exists()); $this->assertTrue($dog1->exists()); $this->assertTrue($dog2->exists()); $this->assertEquals(1, Human::get()->Count()); $this->assertEquals(2, Dog::get()->Count()); $this->assertEquals($owner->ID, $dog1->OwnerID); $this->assertEquals($owner->ID, $dog2->OwnerID); }
public function actionRemove() { $humanId = (int) Yii::app()->getRequest()->getPost('id'); $humanModel = new Human(); if ($humanId > 0) { $humanModel->deleteRowById($humanId); } }
public function testBasic() { $testData = ['firstname' => 'Bobby', 'surname' => 'Silver']; $bob = new Human($testData['firstname'], $testData['surname']); $this->assertEquals($bob->getFirstname(), $testData['firstname']); $this->assertEquals($bob->getSurname(), $testData['surname']); $this->assertNotNull($bob->getId()); $this->assertEquals($bob->toArray(), $testData); }
/** * This function does anything we want. * Sometimes we want it to build something. Then, the result will be returned. * * @access public * @author Rasmus <*****@*****.**> * @uses Human for human list */ public static function start() { require_once dirname(__FILE__) . '/Human.php'; $john = new Human(); $john->setName("John")->setAge(23); $mary = new Human(); $mary->setName("Mary")->setAge(18); $bob = new Human(); $bob->setName("Bob")->setAge(6); $people = array($john, $mary, $bob); usort($people, array("Human", "compare")); echo "<pre>"; var_dump($people); "</pre>"; }
/** * @throws \ValidationException * @throws null */ public function testBranchDelete_DeleteManyObjects_ObjectsDeleted() { $objects = array(); for ($i = 0; $i < 100; $i++) { $human = new Human(); $human->Name = 'Proud Owner ' . $i; $human->write(); $dog = new Dog(); $dog->Name = 'Pup ' . $i; $dog->Color = 'Fifty Shade No. ' . $i; $dog->Owner($human); $dog->write(); $objects[] = $human; $objects[] = $dog; } $batch = new \Batch(); $batch->delete($objects); $this->assertEquals(0, Dog::get()->Count()); $this->assertEquals(0, Human::get()->Count()); }
/** * */ public function testWriteManyMany_CreateParentAndChildren_WritesManyMany() { $parent = new Batman(); $parent->Name = 'Bruce Wayne'; $parent->Car = 'Bat mobile'; $children = array(); for ($i = 0; $i < 5; $i++) { $child = new Child(); $child->Name = 'Soldier #' . $i; $children[] = $child; } $batch = new \Batch(); $batch->write(array($parent)); $batch->write($children); $sets = array(); foreach ($children as $child) { $sets[] = array($parent, 'Children', $child); } $batch->writeManyMany($sets); $parent = Human::get()->first(); $this->assertEquals(5, $parent->Children()->Count()); }
public function __construct($name, $age, $boobs, $butt) { parent::__construct($name, $age); $this->boobs = $boobs; $this->butt = $butt; }
return $much; } } public function showMoney(){ return $this->money; } public function showBank(){ return $this->bank; } } $lisi = new Human(); $flag = $lisi->send(2000); if($flag){ echo '借了',$flag,'元<br />'; echo '还剩',$lisi->showMoney(),'元<br />'; echo '银行还有',$lisi->showBank(),'元<br />'; } ?>
} public function run(){ echo '走'; } public function cry(){ echo '哭'; } public function think(){ echo '思考'; } } $lijun = new Human(); $lijun->eat(); //吃 $lijun->think(); //思考 echo '<hr />'; /* 根据接口,造一个天使 */ class Angle implements animal,monkey,wisdom,bird{ public function eat(){ echo '吃'; } public function run(){
<?php class Human { const HANDS = 2; function printHands() { echo "<br>" . self::HANDS; //звернення до константи в класі } } echo "К-ть рук - " . Human::HANDS; //звернення до константи поза класом $a = new Human(); $a->printHands(); //константу класу буде видно тільки із цього класу //і її неможливо буде змінити
<?php header('Content-Type:text/html;charset=utf-8'); //include('./25.php');或require此时可能出现不同人多次include同一个文件 //once可以解决上面的问题但是once效率很低 // function __autoload($c){ // require('./'.$c.'.php'); // } spl_autoload_register('zdjz'); function zdjz($c) { require './' . $c . '.php'; } $m = new Human(); $m->t(); // function test() { // class Bird { // public static function sing() { // echo "百灵鸟放声歌唱"; // } // } // } // //Bird::sing();//Class 'Bird' not found因为没有调用test()函数 // test(); // Bird::sing();
try { $pdo_object = new PDO('mysql:host=localhost;dbname=Challenge_db;charset=utf8', 'sakamoto', 2591); } catch (PDOException $E) { die('接続に失敗しました:' . $E->getMessage()); } //SQL文を格納した文字列を定義 $sql = "select * from {$this->table}"; //実行とその結果を受け取れる変数を用意 $query = $pdo_object->prepare($sql); //SQLを実行 $query->execute(); while ($row = $query->fetch(PDO::FETCH_OBJ)) { echo $row->stationID; echo $row->stationName . "<br>"; } } //初期化処理 public function __construct() { $this->table = 'station'; } } $profile = new Human(); $profile->show(); $station = new Station(); $station->show(); $pdo_object = null; ?> </body> </html>
public function etc(){ echo '吃饭<br />'; } public function intro(){ echo '找不到工作'; //echo $this->name; } } Human::cry(); //静态调用方法 Error_reporting(E_ALL|E_STRICT);// 开启报错 Human::etc(); //调用非静态的方法则会提示:Strict Standards: Non-static method Human::eat() should not be called statically /* 类->访问->静态方法 可以 类->动态方法 方法内没有this的情况下,但严重不支持.逻辑上解释不通. 对象-->访问动态方法 可以 对象-->静态方法 可以 */ ?>
require_once 'classes/Whales.php'; } // Create object with varScopes class to demonstrate variable visibility $ScopeProof = new varScopes(); // From this vantage we cannot reach varProtected1 or varPrivate1 // Only $varPublic1 will be accessible echo '<div class="alert alert-success" role="alert">'; echo 'From main, outside of class: '; echo $ScopeProof->varPublic1 . '.'; echo '</div>'; // Call parentFunction on ScopeProof $ScopeProof->parentFunction(); // Instantiate classes for Whale, Ape, and Human to create objects $Henry = new Whale(); $Steve = new Ape(); $Henrietta = new Human(); ?> <div class="row"> <div class="col-md-4 col-md-offset-4 bg-info" id="primary"> <h2>Proof that I now understand PHP Interfaces</h2> <div class="list-group"> <p class="list-group-item"><?php echo $Henry->eats(); ?> </p> <p class="list-group-item"><?php echo $Henry->drinks(); ?> </p> <p class="list-group-item"><?php echo $Henry->plays();
public function Say(){ echo 'Hello, My name is',$this->name,' My Sex is',$this->gender; //函数内调用必须使用$this } //PHP函数也不能被重载 /* public function Say($hi){ echo 'Hello, My name is',$this->name,' My Sex is',$this->gender; //函数内调用必须使用$this echo '<br />', $hi; } */ } $p1 = new Human('牛xx', '男'); $p2 = new Human('空姐', '女'); //$p3 = new Human('小xx', '男', 18) $p4 = new Human('性感小姐', '女'); $p1->Say(); echo '<br />'; $p2->Say(); echo '<br />'; //$p3->Say(); //Fatal error: Cannot redeclare Human::__construct() $p4->Say(21); ?>
public function changeValue(){ Human::$head = 2; }
<?php require_once dirname(__FILE__) . '/Creature.php'; require_once dirname(__FILE__) . '/Human.php'; require_once dirname(__FILE__) . '/Dog.php'; $human = new Human("John", 22); $human->greet(); $dog = new Dog(2, 2.4); $dog->greet();
/** * */ public function testWriteManyMany_SetChildrenForParent_RelationWritten() { $parent = new Human(); $parent->Name = 'Bob'; $children = array(); for ($i = 0; $i < 5; $i++) { $child = new Child(); $child->Name = 'Soldier #' . $i; $children[] = $child; } $writer = new \BatchedWriter(); $afterExists = new \OnAfterExists(function () use($writer, $parent, $children) { $writer->writeManyMany($parent, 'Children', $children); }); $afterExists->addCondition($parent); $afterExists->addCondition($children); $writer->write(array($parent)); $writer->write($children); $writer->finish(); $parent = Human::get()->first(); $this->assertEquals(5, $parent->Children()->Count()); }
<?php header('Content-Type:text/html;charset=utf-8'); class Human { public function hell() { echo "hello<br>"; } public function __call($method, $arguement) { echo '你想调用一个我不存在的方法' . $method; echo '传的参数是<br>'; print_r($arguement); } // public function __callStatic() { // } protected function ma($a, $b) { } } $h = new Human(); $h->say('lily', '女'); $h->ma(1, 2);
public function changehead() { self::$header = 9; }
{ //在抽象方法前面添加abstract关键字可以表明这个方法是抽象方法 不需要具体实现 public abstract function eat($food); //抽象类中可以包含普通方法,有方法的具体实现。 public function breath() { echo "Breath use the air \n <br/>"; } } //继承抽象类的关键字是extends class Human extends ACanEat { //继承抽象类的子类需要实现抽象类中定义的抽象的方法 public function eat($food) { echo "Human eating " . $food . "<br/>"; } } class Animal extends ACanEat { public function eat($food) { echo "Animal eating " . $food . "<br/>"; } } $man = new Human(); $man->breath(); $man->eat("apple"); $animal = new Animal(); $animal->breath(); $animal->eat("bananer");
<?php header('Content-Type:text/html;charset=utf-8'); /**/ class Human { public $name = "张三"; public static function cry() { echo "5555<br/>"; } public function eat() { echo "吃饭<br/>"; } public function intro() { echo $this->name; } } Human::cry(); Human::eat(); //Human::intro();//提示:Using $this when not in object context $jason = new Human(); $jason->cry(); //动态调用静态还是可以的
public function show() { parent::show(); echo 'world'; }
public function changeHead() { Human::$name = 9999; }
<head> <meta charset="UTF-8"> <title>private、protected、public三者的区别</title> </head> <body> <?php /* private protected public 三者的区别 */ header('Content-Type:text/html;charset=utf-8'); class Human { private $name = 'jason'; protected $money = 3000; public $age = 21; public function say() { echo '我叫' . $this->name . ',今年我' . $this->age . ',我有' . $this->money . '元钱<br>'; } } $lisi = new Human(); $lisi->say(); echo $lisi->age; ?> </body> </html>
public function __construct($name, $age, $dick, $balls) { parent::__construct($name, $age); $this->dick = $dick; $this->balls = $balls; }
{ } protected abstract function live(); protected abstract function communicate($tool); } class Animals extends Living { protected function live() { echo 'Search food ... ' . PHP_EOL; } public function communicate($tool, $cantalk = false) { echo 'Animals can\'t use' . $tool . ' and they ' . ($cantalk ? 'can' : 'can\'t') . ' talk.' . PHP_EOL; } } class Human extends Living { protected function live() { echo 'Work and make money ...' . PHP_EOL; } public function communicate($tool, $cantalk = true) { echo 'Human beings can use ' . $tool . ' and they ' . ($cantalk ? 'can' : 'can\'t') . ' talk.' . PHP_EOL; } } $animal = new Animals(); $animal->communicate('phone', false); $human = new Human(); $human->communicate('phone', true);
常量:在类内用使用const声名即可 前面不用加修饰 而且权限是public的,即外部也可以访问 */ define('ACC', 'Deny'); define('NEWS', 'Cnet'); class Human{ const HEAD = 1; public static $leg = 2; public static function show(){ echo ACC,'<br />'; echo self::HEAD,'<br />'; echo self::$leg,'<br />'; } } Human::show(); echo Human::HEAD; //访问常量HEAD echo NEWS; ?>
<HTML> <HEAD> <TITLE>クラスのテスト</TITLE> </HEAD> <BODY> <?php class Human { public $tall = 10; public $weight = 20; public function PRIVACY($tall1, $weight1) { $this->tall = $tall1; $this->weight = $weight1; } public function SHOWPRIVACY() { "私の身長は" . (print $this->tall . "です。<br>"); "私の体重は" . (print $this->weight . "です。<br>"); } } $newHuman = new Human(); $newHuman->PRIVACY(178, 60); $newHuman->SHOWPRIVACY(); ?> </BODY> </HTML>