function testIsRemoved() { $this->setUp(); // Remove model to trash. $fruit = Fruit::model()->findByPk(1); $fruit->remove()->save(); $fruit = Fruit::model()->withRemoved()->findByPk(1); $this->assertTrue($fruit->getIsRemoved()); $this->assertTrue($fruit->isRemoved); }
/** * test HABTM saving when join table has no primary key and only 2 columns. * * @return void */ public function testHabtmSavingWithNoPrimaryKeyUuidJoinTable() { $this->loadFixtures('UuidTag', 'Fruit', 'FruitsUuidTag'); $Fruit = new Fruit(); $Fruit->FruitsUuidTag->order = null; $data = array('Fruit' => array('color' => 'Red', 'shape' => 'Heart-shaped', 'taste' => 'sweet', 'name' => 'Strawberry'), 'UuidTag' => array('UuidTag' => array('481fc6d0-b920-43e0-e50f-6d1740cf8569'))); $result = $Fruit->save($data); $this->assertFalse(empty($result)); }
public function testCallingParentOnStatics() { $this->assertEquals('Fruit', Fruit::getName()); $this->assertEquals('Fruit: Apple', Apple::getName()); }
{ protected $name; function _construct($name) { $this->name = $name; } static function getDefinition() { return "Food is good"; } function formatName() { return "I love " . $this->name; } } class Fruit extends Food { function formatName() { return Food::formatName() . "(fruit)"; } } $fruit = new Fruit("cherry"); echo $fruit->formatName(); echo Fruit::getDefinition(); //printing $medals = array("US" => array(1, 2, 3), "UK" => array(1, 2, 3), "CH" => array(1, 2, 3)); foreach ($medals as $country => $counts) { echo sprintf("<tr>\n \t\t <td>%s</td>\n \t\t <td>%d</td>\n \t\t <td>%d</td>\n \t\t <td>%d</td>\n \t\t </tr>", htmlentities($country), $counts[0], $counts[1], $counts[2]); printf("this is equivalent"); }
{ private $type; private static $types = array(); private function __construct($type) { $this->type = $type; } public static function getFruit($type) { // Lazy initialization takes place here if (!isset(self::$types[$type])) { self::$types[$type] = new Fruit($type); } return self::$types[$type]; } public static function printCurrentTypes() { echo 'Number of instances made: ' . count(self::$types) . "\n"; foreach (array_keys(self::$types) as $key) { echo "{$key}\n"; } echo "\n"; } } Fruit::getFruit('Apple'); Fruit::printCurrentTypes(); Fruit::getFruit('Banana'); Fruit::printCurrentTypes(); Fruit::getFruit('Apple'); Fruit::printCurrentTypes();
public function consume() { $this->peel(); $this->slice(); $this->eat(); } } class Grape extends Fruit { public function peel() { echo "<p>No need to peel a grape!</p>"; } public function slice() { echo "<p>No need to slice a grape!</p>"; } } echo "<h2>Consuming an apple...</h2>"; $apple = new Fruit; $apple->consume(); echo "<h2>Consuming a grape...</h2>"; $grape = new Grape; $grape->consume(); ?> </body> </html>
<?php use G\Enum; class Fruit extends Enum { const Apple = 1; const Pear = 2; const Banana = 3; const Orange = 4; const Peach = 5; } class Exotic extends Fruit { const Papaya = 1; const Starfruit = 6; const Mango = 7; } $lunch1 = new Fruit('Apple'); $lunch2 = new Fruit(Fruit::Orange); $dinner1 = new Exotic('Mango'); $dinner2 = new Exotic(Exotic::Papaya); echo $lunch1, PHP_EOL; echo $lunch2->getName(), PHP_EOL; echo $dinner1, PHP_EOL; echo $dinner2->getName(), PHP_EOL; var_dump($lunch1->getElements()); var_dump($lunch1); var_dump($dinner2);