Page 1 of 1

Abstract class oddity..

Posted: Thu Jul 27, 2006 4:22 pm
by Jenk
Am I doing something wrong?

The abstract class:

Code: Select all

<?php

abstract class AbstractView
{
	protected $template;
	protected $smarty;
	protected $model;
		
	protected function __construct (Smarty $smarty, iModel $model)
	{
		$this->smarty = $smarty;
		$this->model = $model;
		
		$this->smarty->caching = false;
		$this->smarty->config_dir = realpath('includes/config');
		$this->smarty->compile_dir = realpath('includes/templates_c');
		$this->smarty->template_dir = realpath('includes/templates');
	}
	
	public function setTemplate ($template)
	{
		if ($this->smarty->template_exists($template)) {
			$this->template = $template;
		} else {
			throw new FileNotFoundException ('Template file not found: ' . $template);
		}
	}
	
	public function assignVariable ($var, $val)
	{
		$this->smarty->assign($var, $val);
	}
	
	public function displayPage ()
	{        
        if (!is_null($this->template)) {    
			$this->smarty->display($this->template);
		}
	}
}

?>
The child:

Code: Select all

<?php

class jmt_IndexView extends AbstractView implements iView
{
    public function __construct (Smarty $smarty, iModel $model)
    {
        parent::__construct($smarty, $model);
        $this->setTemplate('index.tpl');
        $this->getNews();
        $this->getUserData();
    }
    
    protected function getNews ()
    {
        $this->assignVariable('news', $this->model->getNews());
    }
    
    protected function getUserData ()
    {
        foreach ($this->model->getUserData() as $var => $val) {
            $this->assignVariable($var, $val);
        }
    }
}

?>
The script:

Code: Select all

<?php
try {
    $db = new jmt_Database( $CONFIG['DB']['HOST'], 
                            $CONFIG['DB']['USER'], 
                            $CONFIG['DB']['PASS'], 
                            $CONFIG['DB']['DATABASE']
                            );
} catch (Exception $e) {
    var_dump($e);
}

if ((!empty($_COOKIE['stkUserId'])) && (!empty($_COOKIE['stkPass']))) {    

    try {
        $user = new jmt_User($db);
        $user->loginWithId($_COOKIE['stkUserId'], $_COOKIE['stkPass']);
        $model = new jmt_IndexModel($db, $user);
    } catch (Exception $e) {
        var_dump($e);
    }
    
} else {
    try {
        $model = new jmt_IndexModel($db);
    } catch (Exception $e) {
        var_dump($e);
    }
}

$view = new jmt_IndexView(new Smarty(), $model);
var_dump($view);
//$view->displayPage();
?>
The output:

Code: Select all

object(jmt_IndexView)#4 (0) { }
er?!

EDIT: corrected a syntax error, but still the same output.

Posted: Thu Jul 27, 2006 4:37 pm
by feyd
..and if you remove the abstract keyword it dumps those properties?

Posted: Thu Jul 27, 2006 4:39 pm
by Jenk
same output whether abstract or not.

Weird that I'm not getting any errors at all.. I've just triple checked E_ALL And display errors.. both on.

Posted: Thu Jul 27, 2006 4:41 pm
by feyd
well, they are protected, i.e. not readable by the outside world.. that could be it.

Posted: Thu Jul 27, 2006 4:44 pm
by Jenk
I've done var_dump()'s on other objects before and they read "[foo:protected] string(9) 'whatever.'" however, the reason I have dumped the $view object is because the call to $view->displayPage() spews an undefined method error.

EDIT: just tried with everything public, still the same :|

Posted: Thu Jul 27, 2006 4:57 pm
by feyd
I can't remember how var_dump() works internally, so I can't say for sure.

Try running this in 5.2 RC1.

Posted: Thu Jul 27, 2006 5:09 pm
by Jenk
Still the same. Will leave it until tomorrow.. (11pm now)

Posted: Thu Jul 27, 2006 5:20 pm
by Chris Corbyn
Odd.

Code: Select all

[root@iris-1 classes]# cat foo.php
<?php

abstract class a
{
    protected $foo = 42;
}

class b extends a
{
    //
}

$x = new b;

var_dump($x);

?>
[root@iris-1 classes]# php foo.php
object(b)#1 (1) {
  ["foo:protected"]=>
  int(42)
}
[root@iris-1 classes]#

Posted: Fri Jul 28, 2006 3:29 am
by Jenk
Sorted.

Embarrassingly, it was because I had the line:

Code: Select all

class jmt_IndexView {}
in my config file, and because I am using __autoload, there was no indication that it was including the real class def file.

:oops: