Jump to content

What am I doing wrong?


LemonInflux

Recommended Posts

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

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');

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.