LemonInflux Posted September 6, 2008 Share Posted September 6, 2008 Quick question - I have this code: error_reporting(E_ALL | E_STRICT); define('DS', DIRECTORY_SEPARATOR); define('ROOT_PATH', dirname(__FILE__)); function __autoload($class_name) { $class_path = implode(DS, explode('_', $class_name)); include_once ROOT_PATH . DS . 'modules' . DS . $class_path .'.php'; } new graph_module('scatter'); This should then, in turn, go collect /modules/graph/module.php, which it does. Inside module.php, I have... class graph_module { private $supportedTypes = array('scatter'); private $builder; public function __construct($graphType) { if(!in_array($graphType, $this->supportedTypes)) { throw new Exception("Graph type not supported."); } $this->builder = 'graph_builder_'. $graphType; return new $this->builder(); } } When I run this code, which, in theory, should output a blank page (graph_builder_scatter is a blank class), I get the following: Warning: Missing argument 1 for graph_module::__construct(), called in C:\xampp\htdocs\modules\graph\module.php on line 25 and defined in C:\xampp\htdocs\modules\graph\module.php on line 19 Notice: Undefined variable: graphType in C:\xampp\htdocs\modules\graph\module.php on line 20 Fatal error: Uncaught exception 'Exception' with message 'Graph type not supported.' in C:\xampp\htdocs\modules\graph\module.php:21 Stack trace: #0 C:\xampp\htdocs\modules\graph\module.php(25): graph_module->__construct() #1 C:\xampp\htdocs\index.php(31): graph_module->__construct('scatter') #2 {main} thrown in C:\xampp\htdocs\modules\graph\module.php on line 21 Anyone any ideas? I can't work it out. I defined the variable in the constructor argument, and the graph type should be supported.. Thanks in advance ---------------- Now playing: Linkin Park - Murmullo via FoxyTunes Link to comment https://forums.phpfreaks.com/topic/123065-what-am-i-doing-wrong/ Share on other sites More sharing options...
Ken2k7 Posted September 7, 2008 Share Posted September 7, 2008 new graph_module('scatter'); That's a valid command line by itself? Link to comment https://forums.phpfreaks.com/topic/123065-what-am-i-doing-wrong/#findComment-635601 Share on other sites More sharing options...
genericnumber1 Posted September 7, 2008 Share Posted September 7, 2008 yes, you should be able to create a new class without assigning to a variable... as for his problem? I have no idea... everything seems to be in order. (except I'm 99.9% sure that you can't return another class in a construct like he is doing.) Link to comment https://forums.phpfreaks.com/topic/123065-what-am-i-doing-wrong/#findComment-635602 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 new graph_module('scatter'); That's a valid command line by itself? Yes. I think the problem is with his __autoload() somehow, because if I just did this without autoloading, it works. I did this: <?php class graph_module { private $supportedTypes = array('scatter'); private $builder; public function __construct($graphType) { if(!in_array($graphType, $this->supportedTypes)) { throw new Exception("Graph type not supported."); } $this->builder = 'graph_builder_'. $graphType; return new $this->builder(); } } class graph_builder_scatter { } new graph_module('scatter'); Link to comment https://forums.phpfreaks.com/topic/123065-what-am-i-doing-wrong/#findComment-635605 Share on other sites More sharing options...
LemonInflux Posted September 7, 2008 Author Share Posted September 7, 2008 Oddly enough, it's not autoloading. I changed the graph_module class' __construct function to getInstance(), and changed new graph_module('scatter'); to $graph = new graph_module(); $graph->getInstance('scatter'); and it works fine. Hmmmm.... Bug maybe? ---------------- Now playing: Biffy Clyro - Now I'm Everyone via FoxyTunes Link to comment https://forums.phpfreaks.com/topic/123065-what-am-i-doing-wrong/#findComment-635725 Share on other sites More sharing options...
LemonInflux Posted September 7, 2008 Author Share Posted September 7, 2008 Oh, and DarkWater...Dance the night away - Van Halen? ---------------- Now playing: Biffy Clyro - Semi-Mental / 4/15ths via FoxyTunes Link to comment https://forums.phpfreaks.com/topic/123065-what-am-i-doing-wrong/#findComment-635726 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 Oh, and DarkWater...Dance the night away - Van Halen? ---------------- Now playing: Biffy Clyro - Semi-Mental / 4/15ths via FoxyTunes Yeah. I just remembered that someone PM'd me and told me, but he used Google. =P I need to change it now though because you got it. xD I love that song though. Link to comment https://forums.phpfreaks.com/topic/123065-what-am-i-doing-wrong/#findComment-635827 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.