4

Is it possible to pass an object into the constructor of a PHP class, and set that object as a global variable that can be used by the rest of the functions in the class?

For example:

class test {

   function __construct($arg1, $arg2, $arg3) {
      global $DB, $ode, $sel;

      $DB = arg1;
      $ode = arg2;
      $sel = $arg3;
   }

   function query(){
      $DB->query(...);
   }

}

When I try to do this, I get a "Call to a member function on a non-object" error. Is there anyway to do this? Otherwise, I have to pass the objects into each individual function directly.

Thanks!

4 Answers 4

6

You probably want to assign them to values on $this.

In your constructor, you'd do:

$this->DB = $arg1;

Then in your query function:

$this->DB->query(...);

This should similarly be done with the other arguments to your constructor.

$this in an instance context is how you reference the current instance. There's also keywords parent:: and self:: to access members of the superclass and static members of the class, respectively.

Sign up to request clarification or add additional context in comments.

3 Comments

That works, thanks for your help! What if I want to do the same thing with non-objects as well? Let's say I want to pass in an integer or a string?
Works the same way. You can put any fields you want on $this, so long as it's a valid identifier (similar to valid variable names). So you would do $this->name = "somestring"; or $this->name = $name; etc. This is not limited to the constructor, any methods on the class can modify fields on $this.
Just for the sake of your code's clarity, add this: private $db = null; and then place a comment on it (maybe not really necessary since $db should tell you the purpose of the property) :)
3

As a side-note...
Even thought this isn't required, it is generally considered best to declare member variables inside the class. It gives you better control over them:

<?php
class test {
    // Declaring the variables.
    // (Or "members", as they are known in OOP terms)
    private $DB;
    protected $ode;
    public $sel;

    function __construct($arg1, $arg2, $arg3) {
      $this->DB = arg1;
      $this->ode = arg2;
      $this->sel = $arg3;
    }

    function query(){
      $this->DB->query(...);
    }
}
?>

See PHP: Visibility for details on the difference between private, protected and public.

Comments

1

you can do it pretty easily by storing the argument as a property of the object:

function __construct($arg1, $arg2, $arg3) {
   $this->db = arg1;
}

function f()
{
  $this->db->query(...);
}

Comments

1

let's say you have a db object

$db = new db();

and another object:

$object = new object($db);

class object{

    //passing $db to constructor
    function object($db){

       //assign it to $this
       $this-db = $db;

    }

     //using it later
    function somefunction(){

        $sql = "SELECT * FROM table";

        $this->db->query($sql);

    }

}

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.