steve
steve

Reputation: 290

overloading magic methods in child classes

Fairly certain this is not a duplicate... here it goes.

My goal here is to overload multiple magic methods in a debug class, and have my main classes extend said class. My thought is this will allow me to have debugging step through the application stack, without me having to overload the magic methods in every class one by one.

So far the code looks something like this

class magicdbg
{
//data container object
private $data;

public function __construct()
{
    require_once("data.php");
    $this->data = data::defInstance();
}

public function __call($name,$arguments)
{
    echo "overloading call <br />";
    $this->data->dbgmsg("Calling method: " . __METHOD__ . " -- with arguments: (" .implode(',',$arguments) . ")");
}

public function __get($var)
{

}
}

and a class extending it

require_once("magicdbg.php");
class salesCtrl extends magicdbg
{
private $data;
public function __construct()
{
    require_once('data.php');
    $this->data = data::defInstance();
    $this->testcall();

}

private function testcall()
{
    echo "test";
}
}

and finally the class that contains the method that magicdbg is calling

class data
{
//application settings
CONST DEBUG = true;

//application messages
//debug messages
private $dbgmsgs;
private $errormsgs = array();
//application objects

//singleton object instance
private static $instance;

public function __construct()
{
    //if debug is enabled instantiate the debug message array
    if(self::DEBUG)
        $this->dbgmsgs = array();
}

public static function defInstance()
{
    echo "defining instance";
    if(!self::$instance)
        self::$instance = new data();

    echo "<br />instance defined<br />";
    return self::$instance;
}

public function dbgmsg($msg)
{
    echo "calling dbgmsg <br />";
    if(self::DEBUG)
        $this->dbgmsgs[] = $msg;
}

public function outputDbg()
{
    if(!self::DEBUG)
        return false;
    echo "testing dbg output <br />";
    $return = '<div id="debug"><ul>';
    foreach($this->dbgmsgs as $msg)
        $return .= "<li>" . $msg . "</li>";

    $return .= "</ul></div>";
    return $return;
}
}

the code is instantiated like so

require_once('sales.ctrl.php');
$ctrl = new salesCtrl();

and finally... my problem seems to be that it doesnt even hit the magic methods defined in the magicdbg class. I assume it doesnt work as I would expect. this might just be my lack of understanding of magic methods and overloading, as well as inheritance. can someone fill me in on what exactly is going wrong? or maybe even a better way to do what im intending?

Upvotes: 1

Views: 795

Answers (1)

ilanco
ilanco

Reputation: 9957

_call is only executed for methods that are not defined in your class. It's like a catch all for undefined methods.

Upvotes: 2

Related Questions