By using this site you agree to the use of cookies.

Learn more

Share via:

PHP: Accessing Other Classes Properties and Methods

We can access other classes properties (variables) and methods (functions) by injecting the class object in the class that needs it.

Edited: 2017-04-30 16:12

PHP article image.

In object orientated PHP (OOP), it may sometimes be desired to use a method from another class. In this article we will discuss two ways to solve this problem.

Problem: A common OOP beginner problem is how to access properties (variables), and methods (functions) from other classes, without the need to extend a class. We actually have a few ways we can deal with this problem. The first, and recommended solution, is to use something called dependency injection. Dependency injection simply means that you make one class depend on another to run. In code, this looks like:

$db = new db_client();
$handle_users = new handle_users($db);

In this example, db_client() is an independent class, shared by two or more projects. It should not be extended. Instead of extending it, we simply inject it in the class that needs database access.

Dependency injection in OOP

A recommended way to make one class depend on another, is by using constructor injection. The solution we discuss in this section will work in most situations. Let us start by imagining a independent class, shared between different projects:

class db_client {
  public $db_link;

  private $db_host = "localhost";
  private $db_user = "Username";
  private $db_pw = "Password";
  private $db_name = "Database";

  public function __construct() {
    $this->db_link = @new mysqli($this->db_host, $this->db_user, $this->db_pw, $this->db_name);
  }
}

The contents of the above db_client class is not really important to this tutorial. Imagine the class is a simple client to handle all the database stuff. It is shared by many different projects. So, we would like it to be independent. In other words, the db_client should not be extended, as extending it would make it difficult to use it in other projects (lower portability).

Now, imagine we also have a handle_users class that deals with everything user related, such as creation of new users, logging in and out, etc. The handle_users class depends on our db_client class above to function, and we would like properties and methods to be accessible from within handle_users. The way to accomplish this is to pass the object of the db_client as a parameter when initializing the handle_users class.

class handle_users {
  public $db;
    
  public function __construct($db) {
    $this->db = $db; // Make $db available via "$this->db->your_function()"
  }
  public function  {
    $this->db->db_link->query('SELECT * FROM....'); // The db_client object used inside handle_users.
  }
}

For this to work, we will need to first create an instance of the db_client class, and then pass it as a parameter when loading the handle_users class. I.e.:

require $_SERVER["DOCUMENT_ROOT"] . 'db_class.php'; // Handles the Database stuff
require $_SERVER["DOCUMENT_ROOT"] . 'handle_users.php'; // All user related stuff

$db = new db_client();
$handle_users = new handle_users($db);

The __construct method is automatically executed when you create an instance of your class, so the $db object automatically becomes available.

Using singleton in PHP

Another way to access methods in another class, is to use singleton. This also makes it possible to use methods, without the need to first instantiate the class, the object is instead created inside the class itself.

Note. Using singleton in PHP is not recommended.

A database class in singleton looks like this:

class db_client {
  private static $instance = null;

  // Credentials
  private $db_host = "localhost";
  private $db_user = "Username";
  private $db_pw = "Password";
  private $db_name = "Database";

  public static function getInstance() {
    if (self::$instance == null) {
      self::$instance = new Singleton();
    }
    return self::$instance;
  }
  public function getConnection() {
  	return $this->db_link;
  }

  private function __construct() {
    $this->db_link = @new mysqli($this->db_host, $this->db_user, $this->db_pw, $this->db_name);
  }
}

To perform a database query, simply do like this:

$db = db_client::getInstance();
$mysqli = $db->getConnection(); 
$result = $mysqli->query("SELECT foo FROM .....");

The __construct() method is declared private, which prevents creating an object from outside the class using the new operator. The object is instead created from within the getInstance() method.