-2

I'm trying to use 2 methods in my class.

the class structure looks something similar to this

class Test extends Something
{
    private $dir;

    public function __construct()
    {
        parent::__construct();
    }

    private function check_theme()
    {
        // do some checking here
        $this->dir = // outcome of the checking
    }

    public function load_theme()
    {
        $this->check_theme();
    }

    public function load_file()
    {
        $this->dir . $path . $to . $file
    }
}

ok so now, this only works if I run something like

$test = new Test();

$test->load_theme();
$test->load_file();

but now I have a different method that I want to access directly but I really want for that method to already know what the value of $dir is.

so if I used $test->load_file(); It would load the file because the value of $dir was already set by $test->check_theme();

2
  • private $dir = $GLOBALS['dir'] ? just a thought .. never tried it
    – sdolgy
    Jul 2, 2011 at 6:03
  • 3
    The code you've posted can already access $dir from other methods. I can't tell what your question is.
    – Brad Mace
    Jul 2, 2011 at 6:04

1 Answer 1

1

I hope I get your question properly. I suggest to keep a flag for when the value of $dir is set.

Here before using the $dir value, we do the test to ensure its value is set. If not we call $load_theme() which calls check_theme() just in the same sequence as you showed in your question.

class Test extends Something
{
    private $dir;
    private $file_loaded;

    public function __construct()
    {
        parent::__construct();
        $this->file_loaded = false;

    }

    private function check_theme()
    {
        // do some checking here
        $this->dir = // outcome of the checking
    }

    public function load_theme()
    {
        $this->check_theme();
        $this->file_loaded = true;   
    }

    public function load_file()
    {
        if (!$this->file_loaded)
            $this->load_theme();            
        $this->dir . $path . $to . $file
    }
}
1
  • hmmm, ya something like that might help in the checking. I was just wondering if I would be able to access the $dir from any method without needing to first run the check_theme(); method over and over again
    – Eli
    Jul 2, 2011 at 6:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.