2

I would like to know if there is a way to access a variable defined in an other file from a class in PHP.

Example :

file_01.php

<?php
    $a = 42;
?>

file_02.php

<?php
    require_once('file_01.php');

    class mnyClass
    {
        private $myVar;

        function __construct($var = $a)
        {
            $this->myVar = $var;
        }

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

        function setVar($var)
        {
            $this->myVar = $var;
        }
    }
?>

Obviously, my class is more complicated. I have chosen this example for a better comprehension of what I try to do ;)

Thank you in advance.

1
  • First of all, I thank you all for your answers. Indeed, I wanted to set up application defaults, but with association arrays. The define() method does not take an array as parameter. I knew the $GLOBALS variable but it can't be used in constructor parameters : / So, I resign to use $GLOBALS in my constructor with NULL tests on parameters. Commented Dec 6, 2011 at 16:09

4 Answers 4

3

You cannot do this:

    function __construct($var = $a)
    {
        $this->myVar = $var;
    }

What you can do is pass it:

<?php
    require_once('file_01.php');
    $mnyClass = new mnyClass($a);// the torch has been passed!

    class mnyClass
    {
        private $myVar;

        function __construct($var = null)
        {
            $this->myVar = $var;
        }

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

        function setVar($var)
        {
            $this->myVar = $var;
        }
    }
?>

OR you can do this (it is not advisable):

    function __construct($var = null)
    {
        if($var === null) $var = $GLOBALS['a']; //use global $a variable
        $this->myVar = $var;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

out of curiosity, why is it not advisable?
@Frank you cannot scope global variables.
2

You could access the variable via GLOBALS:

http://php.net/manual/en/language.variables.scope.php

EDIT: a little more detail-

function __construct() {
  $this->myVar = $GLOBALS['a'];
}

Comments

1

It sounds like you're setting up some application defaults. It might make sense to define these as constants:

file_01.php:

define('DEFAULT_VALUE_FOR_A', 42);

file_02.php

class myClass
{
    function __construct($var = DEFAULT_VALUE_FOR_A) {
    }
}

Comments

0

Finally, I use this method :

<?php

require_once('file_01.php');

class myClass {

    private $myVar;

    function __construct($var = NULL)
    {
        global $a;

        if($var == NULL)
            $this->myVar = $a;
        else
            $this->myVar = $var; 
    }
}

?>

I declare my variable $a as global in the constructor, set the default value of my $var to NULL and check if the constructor was called with parameter(s) ($var == NULL).

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.