Arkadiusz Galler
Arkadiusz Galler

Reputation: 153

object is treated as non-object

I've got another question.
I've got this source_folder/class/controller/login.class.php file:

<?php

/**
 * Controller that handles logging in
 *
 * @author Lysy
 */

class Controller_Login {

    private $oView;

    public function __construct($action) {
        $sAction = 'action_' . $action;
        $this->$sAction();
        $this->oView = new View_Login();
    }

    public function action_index() {
        echo 'Am I object ? '.(is_object($this->oView)) ? "Yes" : "No";
        $this->oView->showLoginAplet();
    }
}
?>

And this source_folder/class/view/login.class.php file:

<?php

/**
 * View that handles logging in
 *
 * @author Lysy
 */

class View_Login {

    public function __construct() {

    }

    public function showLoginAplet() {
        echo '<form action="" method="POST">'
        . '<input type="text" name="login" value="login" />'
        . '<input type="text" name="pass" value="password" />'
        . '</form>';
    }
}
?>

When I try to load Controller_Login('index') in my index.php file (__autoload works perfectly), I get this result:

Yes
Fatal error: Call to a member function showLoginAplet() on a non-object in D:\Program Files\WebServ\httpd\class\controller\login.class.php on line 21

Why the oView variable is said to be an object (also why "Am I object ? " is not displayed?) and then it is said to be non-object?

Upvotes: 0

Views: 36

Answers (1)

Bart Friederichs
Bart Friederichs

Reputation: 33511

Your order is wrong, when you call $this->$sAction();, $this->oView is not instantiated yet, but you do use it in action_index. Fix like this:

public function __construct($action) {
        $this->oView = new View_Login();
        $sAction = 'action_' . $action;
        $this->$sAction();
}

About the subquestion, this:

echo 'Am I object ? '.(is_object($this->oView)) ? "Yes" : "No";

is evaluated as:

echo ('Am I object ? '.(is_object($this->oView))) ? "Yes" : "No";

which always renders to "Yes" as any non-empty string is considered true.

Upvotes: 2

Related Questions