jacob1986 Posted December 8, 2015 Share Posted December 8, 2015 I have two questions relating to each other which are regarding - both function __autoload and adding products to a class? Code for an autoload function (please see code #1), the output prints - but also gives an error message? Moreover, I also receive the notification in my php editor "class 'ooo' not found"? If I may ask a silly question I have noticed some autoload fucctions have implode and explode - how would I write one of these? The output and error: My Antonia: Willa Cather (5.99)Fatal error: Class 'ooo' not found in C:\ Code#1: <?php// we've writen this code where we needfunction __autoload($classname) { $filename = "./". $classname .".php"; include_once($filename);}// we've called a class ***$obj = new ooo();?> ------------------------------------------------------------------------------------------------------------------------------ How would I add more products to this product class? code#2: <?phpclass shopProduct { public $title; protected $producer = []; public $price; public function __construct($title, $producerName1, $producerName2, $price) { $this->title = $title; $this->producer[] = $producerName1; $this->producer[] = $producerName2; $this->price = $price; } public function getProducer() { return implode(' ', $this->producer); }}class ShopProductWriter { public function write ($shopProduct) { $str = "{$shopProduct ->title}: " . $shopProduct -> getProducer() . " ({$shopProduct -> price})\n"; print $str; }}$product1 = new ShopProduct("My Antonia", "Willa", "Cather", 5.99);$writer = new ShopProductWriter();$writer -> write($product1);?> Quote Link to comment https://forums.phpfreaks.com/topic/299675-function-__autoload/ Share on other sites More sharing options...
Strider64 Posted December 8, 2015 Share Posted December 8, 2015 // Autoload classes from "classes" directory: function class_loader($class) { require("lib/classes/" . $class . ".php"); } spl_autoload_register("class_loader"); Very help link http://php.net/manual/en/function.spl-autoload-register.php Quote Link to comment https://forums.phpfreaks.com/topic/299675-function-__autoload/#findComment-1527682 Share on other sites More sharing options...
maxxd Posted December 8, 2015 Share Posted December 8, 2015 You would use explode() and implode() in an autoloader to use the class's namespace to map a directory structure. For instance, you would map Foo\Objects\MyObject to /Foo/Objects/MyObject.php by exploding on '\' and imploding with '/'. You're getting the 'class not found' error because you're not loading the file that contains the class definition for ooo(). You're not going to add more products to the product class - each instance of the class represents a single product. So, you would have a Shop class that would contain an array of Product classes. (I'm assuming this is from Zandstra's book, as your other post references the book and I remember him using Willa Cather as an example.) I can't remember exactly how he describes using the Product class, but I think he discusses this a bit further on. Quote Link to comment https://forums.phpfreaks.com/topic/299675-function-__autoload/#findComment-1527684 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.