0

the class, which is content.php

class content
{
    private $vars = array();

    public function set($var, $var_value)
    {
        $this->vars[$var] = $var_value;
    }

index.php file using the content class

include 'content.php';
$instance = new content();
$instance->set(image_dir, '/images/');
include 'display.php';

display.php

<?php

echo '<img src={image_dir}/graphic.gif width=556 height=49></td>"';

How do I use the content->var variable in the display.php? which is the best way?

4 Answers 4

3

You should use a getter.

class content
{
    private $vars = array();

    public function set($var, $var_value)
    {
        $this->vars[$var] = $value;
    }

    public function getImageDir()
    {
         return $this->vars['image_dir'];
    }
}

And then:

include 'content.php';
$instance = new content();
$instance->set(image_dir, '/images/');
$image_dir = $instance->getImageDir();
include 'display.php';
Sign up to request clarification or add additional context in comments.

Comments

2

Add a getter to your class:

class content
{
    private $vars = array();

    public function set($var, $var_value)
    {
        $this->vars[$var] = $var_value;
    }

    public function get($var)
    {
        return $this->vars[$var];
    }

}

Then you can use it like:

$instance->get(image_dir);

Note that if image_dir is not a constant IE. define('image_dir', '../images/blah'); then it should be in quotes.

Comments

1

What about using a get() method in your class?

public function get($var)
{
    return $this->vars[$var];
}

Maybe you can have a look to the magic method __call() too

Comments

1

just do in content class:

public function getVar(){
   return $this->vars;
}

and in index.php

echo '<img src='.$content->getVar()['image_dir'].'/graphic.gif width=556 height=49></td>"';

1 Comment

This won't works, except for PHP 5.4 (unfortunately, I still have to wait to use it :( )

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.