1

I would like to ask, if there is a way, to use variables in a class, that were declared out of it.

Example:

$foo = 'bar';
class foobar{
    function example(){
        echo "foo{$foo}";
    }
}
$foobar = new foobar;
$foobar->example();

This code produces a notice: Notice: Undefined variable: foo Is there a way to make it work? Or is there some work-around?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Chris Illusion
  • 277
  • 2
  • 5
  • 18
  • 1
    Yes. Simply make an attribute to the class and build a constructor that sets an argument (namely the variable that you pass in) to the aforementioned attribute. –  Jan 05 '13 at 23:39
  • http://stackoverflow.com/questions/2862399/php-access-class-inside-another-class – Waleed Khan Jan 05 '13 at 23:44

3 Answers3

4

You could give this argument to your class with a constructor

class foobar{    

    private $foo;

    public function __construct($name) {
        $this->foo = $name;
    }
}

and then use it.

Or what PeeHaa means, you could change your method to

function example($param){
    echo "foo{$param}";
}

and call it like this

$foobar->example($foo);
Alex VII
  • 930
  • 6
  • 25
0

Use a construct to import it or use the global keyword. You could do something like this:

$var = 'value';
class foobar {
    private $classVar;
    function __construct($param) {
        $this->classVar = $param;
    }
}

And initiate it like this:

$var = 'value';
$inst = new foobar($var);

Or you can use global variables (which I wouldn't recommend in this case) and do something like this:

$var = 'value';
class foobar {
    global $var;
    function show() {
       echo $var;
    }
}

UPDATE: To use a class within another class, it may be instantiated in the constructor if its instance is needed throughout implementation, or it may be instantiated only when needed.

To create a reference to another class inside the constructor, do something like this:

class class1 {
    private $someVar;

    function __construct() {
        $this->someVar = 'success';
    }

    function doStuff() {
        return $this->someVar;
    }
}

class class2 {
    private $ref;
    private $val;
    function __construct() {
        $this->ref = new class1();
        $this->val = $this->ref->doStuff();
        // $this->val now holds the value 'success'
    }
}

$inst = new class2(); // upon calling this, the $val variable holds the value 'success'

Or you can call it only when needed, like so:

class class1 {
    private $someVar;

    function __construct() {
        $this->someVar = 'success';
    }

    function doStuff() {
        return $this->someVar;
    }
}

class class2 {
    private $ref;
    private $val;
    function __construct() {
        // do something
    }
    function assign() {
        $this->ref = new class1();
        $this->val = $this->ref->doStuff();
        // $this->val now holds the value 'success'
    }
}

$inst = new class2(); // the $val variable holds no value yet
$inst->assign(); // now $val holds 'success';

Hope that helps you.

SISYN
  • 2,209
  • 5
  • 24
  • 45
  • I would be interested to know down-voted this answer and for what reason. I gave more than one way to accomplish the task and demonstrated how to use it instead of just giving half of the code. Some people on this site bewilder me. – SISYN Jan 05 '13 at 23:51
  • Thank you very much for the answer! I find it the most complete :) – Chris Illusion Jan 06 '13 at 00:01
  • Thank you for the acceptance! I'm not sure why people on this site down-vote others for no reason, I was just trying to help. If you need any further help please just let me know and I'll be happy to help you. – SISYN Jan 06 '13 at 00:02
  • I was playing a bit now with this newly gained knowledge and found out, that I can pass even classes like this. So I came out with: $this->foo_class->another_example() and it was actually working. However, what would be the proper way of using a different class in a class? Thank you very much :) – Chris Illusion Jan 06 '13 at 00:29
  • The way you're doing it is correct. Depending on your needs, you can setup the instance of the imported class inside the constructor or you can create it only when needed. I will update my answer showing you how to do both. P.S. If you don't mind, please up-vote my answer if it was helpful, I like having a good reputation on this site. :) – SISYN Jan 06 '13 at 00:34
  • Thanks! I'm trying to up-vote, but it says I need 15 reputation before I can do it :/ If I somehow get the rep, I'll be sure to come back and upvote :) – Chris Illusion Jan 06 '13 at 00:42
  • I have updated the answer now. I also up-voted your question so you should have some reputation score now. Hope that helps you, if you need anything else or if this doesn't work for you please let me know and I'd be happy to help some more! – SISYN Jan 06 '13 at 00:43
  • No problem! Let me know if you need anymore help. If you ever need help feel free to contact me via my website at http://mdl.fm and I will be glad to help! – SISYN Jan 06 '13 at 00:55
  • You cannot use `global $var;` outside of the class method like that. The line should be inside of `function show(){...}` Otherwise good answer! – newms87 Jan 06 '15 at 18:06
-2

Yes add

class foobar{
    function example(){

        global $foo;

        echo "foo{$foo}";
    }
}

** putting it in another class is better though, or passing it to the method you're using is better too **

Shehabic
  • 6,787
  • 9
  • 52
  • 93
  • 1
    [Why `global`s are bad?](http://stackoverflow.com/questions/11923272/use-global-variables-in-a-class/11923384#11923384) – PeeHaa Jan 05 '13 at 23:40
  • 1
    This works, but is bad style, especially when used in classes. – Nic Jan 05 '13 at 23:42