Asked  7 Months ago    Answers:  5   Viewed   36 times

I have a global variable outside my class = $MyNumber;


How do I declare this as a property in myClass?
For every method in my class, this is what I do:

class myClass() {

    private function foo() {
         $privateNumber = $GLOBALS['MyNumber'];
    }

}



I want this

class myClass() {

    //What goes here?
    var $classNumber = ???//the global $MyNumber;

    private function foo() {
         $privateNumber = $this->classNumber;
    }

}



EDIT: I want to create a variable based on the global $MyNumber but
modified before using it in the methods

something like: var $classNumber = global $MyNumber + 100;

 Answers

41

You probably don't really want to be doing this, as it's going to be a nightmare to debug, but it seems to be possible. The key is the part where you assign by reference in the constructor.

$GLOBALS = array(
    'MyNumber' => 1
);

class Foo {
    protected $glob;

    public function __construct() {
        global $GLOBALS;
        $this->glob =& $GLOBALS;
    }

    public function getGlob() {
        return $this->glob['MyNumber'];
    }
}

$f = new Foo;

echo $f->getGlob() . "n";
$GLOBALS['MyNumber'] = 2;
echo $f->getGlob() . "n";

The output will be

1
2

which indicates that it's being assigned by reference, not value.

As I said, it will be a nightmare to debug, so you really shouldn't do this. Have a read through the wikipedia article on encapsulation; basically, your object should ideally manage its own data and the methods in which that data is modified; even public properties are generally, IMHO, a bad idea.

Wednesday, March 31, 2021
 
Blacksonic
answered 7 Months ago
98

Example of using Dependency Injection (DI) to share an instance of Main between to different class instances rather than trying to use inheritence

class Main {
    public $foo;
}

class A {

    protected $main;

  public function setVar($data)
    {
      $this->main->foo = $data;
    }

  public function __construct(Main $main)
    {
      $this->main = $main;
    }
}

class B extends Main {
    protected $main;

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

  public function __construct(Main $main)
    {
      $this->main = $main;
    }
}

$M = new Main;
$A = new A($M);
$B = new B($M);

$A->setVar("test");

echo "result: ".$B->getVar();
Wednesday, March 31, 2021
 
Giovanni
answered 7 Months ago
63

include global $myarray at the start of setvalue() function.

public function setvalue() {
    global $myarray;
    $myvalue = $myarray[0];
}

UPDATE:
As noted in the comments, this is bad practice and should be avoided.
A better solution would be this: https://stackoverflow.com/a/17094513/3407923.

Wednesday, March 31, 2021
 
SubniC
answered 7 Months ago
82

If your child class extends the parent class there's no need to do that, do this instead:

$this->update_query_function();

that's the point of inheritance.

Wednesday, March 31, 2021
 
tompave
answered 7 Months ago
59

To answer your question, the current method would be to create the object then call the method:

$functions = new Functions();
$var = $functions->filter($_GET['params']);

Another way would be to make the method static since the class has no private data to rely on:

public static function filter($data){

This can then be called like so:

$var = Functions::filter($_GET['params']);

Lastly, you do not need a class and can just have a file of functions which you include. So you remove the class Functions and the public in the method. This can then be called like you tried:

$var = filter($_GET['params']);
Thursday, July 29, 2021
 
nighter
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :