How can use class from another class in php?

Hey there I'm wondering how this is done as when I try the following code inside a function of a class it produces some php error which I can't catch

public $tasks;
$this->tasks = new tasks($this);
$this->tasks->test();

I don't know why the initiation of the class requires $this as a parameter either :S

thanks

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();

asked Sep 23, 2009 at 21:31

SupernovahSupernovah

1,88612 gold badges34 silver badges50 bronze badges

2

You can't declare the public $tasks inside your class's method (function.) If you don't need to use the tasks object outside of that method, you can just do:

$tasks = new Tasks($this);
$tasks->test();

You only need to use the "$this->" when your using a variable that you want to be available throughout the class.

Your two options:

class Foo
{
    public $tasks;

    function doStuff()
    {
        $this->tasks = new Tasks();
        $this->tasks->test();
    }

    function doSomethingElse()
    {
        // you'd have to check that the method above ran and instantiated this
        // and that $this->tasks is a tasks object
        $this->tasks->blah();
    }

}

or

class Foo
{
    function doStuff()
    {
        $tasks = new tasks();
        $tasks->test();
    }
}

with your code:

class Admin
{
    function validate()
    {
        // added this so it will execute
        $_SESSION['level'] = 7;

        if (! $_SESSION['level'] == 7) {
            // barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        } else {
            $tasks = new Tasks();
            $tasks->test();
            $this->showPanel();
        }
    }

    function showPanel()
    {
        // added this for test
    }
}
class Tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new Admin();
$admin->validate();

outis

73.2k19 gold badges146 silver badges216 bronze badges

answered Sep 23, 2009 at 21:37

How can use class from another class in php?

Lance KidwellLance Kidwell

8531 gold badge14 silver badges18 bronze badges

0

You're problem is with this line of code:

public $tasks;
$this->tasks = new tasks();
$this->tasks->test();
$this->showPanel();

The public keyword is used in the definition of the class, not in a method of the class. In php, you don't even need to declare the member variable in the class, you can just do $this->tasks=new tasks() and it gets added for you.

answered Sep 23, 2009 at 21:44

davidtbernaldavidtbernal

13.2k9 gold badges44 silver badges59 bronze badges

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

2294 views

How can use class from another class in php?

By. Jacob

Edited: 2020-09-06 10:02

How can use class from another class in php?

In object orientated PHP (OOP), it may sometimes be desired to access methods from another classes, In this article we will discuss two ways to solve this problem.

Problem: A common beginner problem in OOP is how to access properties (variables) and methods (functions) from other classes without extending them. We actually have a few ways we can deal with this problem. The first, and recommended solution, is to use a technique called Dependency injection (DI), this technique allows us to split up our code and organize it in separate classes, which can then be independently instantiated.

See also: One Big Class vs Multiple Classes

When using DI we will usually be instantiating a class once in our composition root (I.e.: index.php), and then pass it on to subsequent classes that depend on it. Here is an example of how this technique may be used:

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

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

Inside handle_users(), the database object will be available in the __construct() magic method:

class handle_users {
  private $db;

  public function __construct($db){
   $this->db = $db;
  }
   
   public function login_check() {
    //  $db->query('');
    // ...
   }
}

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 an independent class that is shared by multiple objects:

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);
  }
}

Think of the db_client() class as a client to handle all the database stuff. Since it may be shared by many different projects or objects, we should try to make it an independent class.

We should not extend the db_client from the handle_users class, as it would make it too tightly coupled with the users-code, and ruin the portability of the db_client() class. Instead, we should use DI to provide the database object as a dependency for the user handler class upon instantiation.

Now, imagine we also make 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 needs the db_client(); class to access the database. To do this, we may pass 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);

__construct(); is a magic method that is automatically executed when you create an instance of your class. The $db object automatically becomes available inside the __construct method where we may assign it to a property (variable).

Using singleton in PHP

Another way to access methods in another class, is to use singleton. This 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.

  1. When using file_get_contents to perform HTTP requests, the server response headers is stored in a reserved variable after each successful request; we can iterate over this when we need to access individual response headers.

  2. How to effectively use variables within strings to insert bits of data where needed.

  3. Flushing and output buffering goes hand in hand, and in this article I try to examine the benefits and disadvantages to flushing.

  4. How to use the AVIF image format in PHP; A1 or AVIF is a new image format that offers better compression than WebP, JPEG and PNG, and that already works in Google Chrome.

  5. How to create a router in PHP to handle different request types, paths, and request parameters.

More in: PHP Tutorials

What is Classin PHP?

A class is a template for objects, and an object is an instance of class.

Can you have a class within a class in PHP?

PHP 7 has introduced a new class feature called the Anonymous Class which will allow us to create objects without the need to name them. Anonymous classes can be nested, i.e. defined inside other classes.

Can we create object inside class in PHP?

Yes, you can create an object from a specific class from inside another class.

How can we use class in another class in PHP?

The public keyword is used in the definition of the class, not in a method of the class. In php, you don't even need to declare the member variable in the class, you can just do $this->tasks=new tasks() and it gets added for you.