1

this is the main class of my project and I am using a array to store classes (not sure if this is the best way), but when using the line below...

$this->getController("config")->loadConfiguration();

It doesn't actually load the config? and when I access it in $this->classes instead of the getController function it says its empty?

I also get this error.. (EDIT) Fatal error: Call to a member function loadConfiguration() on a non-object

Here is my whole class:

<?php
defined("SECURE") or exit('Please define SECURE keyword to continue.');
class miracle
{
    //Usage: $this->getController("test")->run();

    private $classes;

    public function __construct()
    {
        $classes = array(); 
    }

    public function run()
    {
        $this->loadClasses();
        $this->getController("config")->loadConfiguration();
    }

    public function getController($c)
    {
        return $classes[$c];
    }

    private function loadClasses()
    {
        $this->classes["template"] = new template();
        $this->classes["config"] = new config();
    }
}
?>
5
  • so reduce the problem surface: does it call loadConfiguration? (stick an echo/print in there, see what happens). If not, then the issue is that it doesn't call your function, and is not about your code not doing what's inside that function. If it does, then the problem is not about the class structure. Commented Feb 20, 2016 at 21:57
  • $classes is in method scope. $this->classes is in class scope. Commented Feb 20, 2016 at 21:57
  • An echo is all thats in there at the moment, it doesnt echo. Commented Feb 20, 2016 at 21:57
  • I now have this error.. Fatal error: Call to a member function loadConfiguration() on a non-object Commented Feb 20, 2016 at 21:58
  • other people already address your issue, but this is what you'd have found out yourself too if you run through a few steps of "reducing code before posting" - you don't get into the function, so perhaps this->classes isn't working. So you initialise it with content, and then check what it is in run. You'd see it was empty, you reduce further, and go "oh wait, I forgot $this-> in my contructor" probably followed by "did I do that anywhere else? oh I forgot it in getController too" Commented Feb 20, 2016 at 21:59

4 Answers 4

4

You have an error in __constructor and in getController methods:

[...]

private $classes;

public function __construct()
{
    $this->classes = array(); 
}

[...]

public function getController($c)
{
    return $this->classes[$c];
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to return:

public function getController($c)
{
    return $this->classes[$c];
}

You should probably use $this->classes in your constructor as well.

1 Comment

$this gives you scope within the class. When you define the variable within the class you won't use $this, but after that every reference should use $this.
0

The problem is this code

$this->classes["template"] = new template();
$this->classes["config"] = new config();

You're trying to create new objects of non-existing classes. You could use stdClass instead like this

$this->classes["template"]= new stdClass();
$this->classes["template"]->name='template';

Comments

0

Taking a quick look at your code, using your given line never calls the loadClasses() function, assuming this has not been called prior, the classes are never loaded into the array.

Try calling it like this:

$this->loadClasses()->getController("config")->loadConfiguration();

As well as this, your function getController() needs to return the class-wide variable, like this:

    public function getController($c)
    {
        return $this->classes[$c];
    }

The same change also should be made to your class constructor.

This is all working on the assumption that the classes template and config are defined elsewhere within your project, included, and available within the current namespace.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.