Abstract class oddity..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Abstract class oddity..

Post 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.
Last edited by Jenk on Thu Jul 27, 2006 7:10 pm, edited 4 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

..and if you remove the abstract keyword it dumps those properties?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

well, they are protected, i.e. not readable by the outside world.. that could be it.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :|
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Still the same. Will leave it until tomorrow.. (11pm now)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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]#
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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:
Post Reply