Paolo Rossi
Paolo Rossi

Reputation: 2510

PHP call methods of others classes

In my php project I've create 4 classes

CONNECTION

class Mysqliconn {

   public $mysqli;  

   public function __construct(){
      include "dbconfig.php";
      $this->connect($host, $user, $password, $database, $charset);
   }

   public function connect (.....)
      $this->mysqli = new mysqli(......);
   }
}

UPLOAD

class Upload {

     private $db;

     public function __construct( Mysqliconn $db ) {
        $this->db = $db; 
     }

     public function getID($id) {
        echo $id;
     }
}

USERS

class Users {

 private $db;

      public function __construct( Mysqliconn $db ) {
          $this->db = $db; 
      }

      public function getName($name) {
      echo $name;
      }
}

CARS

class Cars {

     private $db;

     public function __construct( Mysqliconn $db ) {
            $this->db = $db; 
     }

     public function getCars($cars) {
        echo $cars;
     }
 }

In my php page I instantiate the classes in this way

function __autoload($class_name) {
    if(file_exists('class/class.' . strtolower($class_name) . '.php')) {
        require_once('class/class.' . strtolower($class_name) . '.php'); 
    }
    else {
        throw new Exception("Unable to load $class_name.");
    }
}

$db = new Mysqliconn();
$up = new Upload($db);
$us = new Users($db);
$cs = new Cars($db);

$cs->getCars('BMW');
$us->getName('Foo');

In my Cars Class I'd like to call method of classes Users and Upload. Is it possibile to do? How could I do this? Thanks.

Upvotes: 2

Views: 103

Answers (2)

Manolo
Manolo

Reputation: 26350

You can call a public static function inside an other class:

class Users {

private $db;

  public function __construct( Mysqliconn $db ) {
      $this->db = $db; 
  }

  public static function getName($name) {
  echo $name;
  }
}


class Cars {
 private $db;

 public function __construct( Mysqliconn $db ) {
        $this->db = $db; 
 }

 public function getCars($cars) {
    echo $cars;
 }

    public function callGetName($name)
   {
         return  Users:: getName($name) ;
   }
}

Another option would be:

function callGetName($name)
 {
   $user = new Users();
   $user->getName($name);
 }

Upvotes: 1

user399666
user399666

Reputation: 19879

class Example{

    public function __construct(){
        $otherClass = new OtherClass();
        $otherClass->functionInOtherClass();
    }

}

or

class Example{

    public function __construct($objFromOtherClass){
        $objFromOtherClass->functionInOtherClass();
    }

}

or

class Example{

    public function __construct(OtherClass $objFromOtherClass){
        $objFromOtherClass->functionInOtherClass();
    }

}

If the object in question will be used throughout your Example class:

class Example{

    protected $objFromOtherClass;

    public function __construct(OtherClass $objFromOtherClass){
        $this->objFromOtherClass = $objFromOtherClass;
    }

    public function test(){
        $this->objFromOtherClass->functionInOtherClass();
    }

}

Upvotes: 2

Related Questions