public function testCreation() { $product = new \Product(); $product->info = array('test'); $this->assertEquals(array('test'), $product->getInfo(), 'Test before saving'); $product->save(); $this->assertEquals(array('test'), $product->getInfo(), 'Test after saving'); $product = \Product::loadOne(2); $this->assertEquals(array('test'), $product->getInfo(), 'Test after loading'); $this->assertTrue(is_array($product->info)); }
public $name = 'default_name'; public $price = 0; public $desc = "default_description"; //static property public static $manufacturer = 'default_maker'; //constructor function __construct($name, $price, $desc) { $this->name = $name; $this->price = $price; $this->desc = $desc; } //method public function getInfo() { return "Product name: " . $this->name; } public function maker() { //$this and -> cannot be used on static methods return self::$manufacturer; } } $product = new Product("shirt", 20, "short-selves"); echo $product->getInfo(); echo '<br><br><br>'; //using the method to get the maker echo $product->maker(); //or use :: to access the method, property echo '<br><br><br>'; echo $product::$manufacturer;
public function getInfo() { //return some info about the Product return "Product Name: " . $this->name; } public function getMaker() { return self::$manufacturer; } } class Soda extends Product { public $flavor; function __construct($name, $price, $desc, $flavor) { parent::__construct($name, $price, $desc); $this->flavor = $flavor; } public function getInfo() { return "Product Name: " . $this->name . " Flavor: " . $this->flavor; } } $shirt1 = new Product("Space Juice T-shirt", 20, "Friggin great t-shirt"); $soda1 = new Soda("Space Juice Soda", 2, "Thirst Mutilator", "Grape"); echo $soda1->getInfo(); echo "<br><br>"; echo $shirt1->getInfo(); echo "<br><br>"; echo $shirt1::$manufacturer; //die();
public $flavor; function __construct($nameNew, $price, $desc, $flavor) { parent::__construct($nameNew, $price, $desc); $this->flavor = $flavor; } public function getInfo() { return "Product Name: " . $this->name . " Flavor: " . $this->flavor; } } //$p = new Product(); // $p->name = "Space and Juice Soda"; BAD PRACTICE // echo $p->name; $shirt = new Product("Space Juice T-Shirt", 20, "Grey T-shirt"); $soda = new Soda("Space Juice Soda", 2, "Thirst Mutilator", "Grape"); echo $shirt->getInfo() . "<br>"; echo $soda->getInfo() . "<br>"; echo $shirt->getMaker() . "<br>"; echo $shirt::$manufacturer; //OOP TOOLS //True if method named getPrice in Product class (or $object) return method_exists("Product", "getPrice"); //subclass $s = new Soda(); is_subclass_of($s, "Product"); //Sub variables for Class Names $class = "Product"; $p = new $class(); $m = "getName"; $name = $p->m();