dimanche 18 octobre 2015

Is it possible to call instantiated static methods in other class in php?

Is it possible to call static methods using Class Instance in the extending classes? I don't know to get the $this->access from parent __construct() all through my classes when extending it so maybe an expert in PHP could help me.

namespace Folder\Subfolder;

class ClassA {  // <-- this class is a custom helper

  public static function accessA() {
    return 'Class A\'s Access';
  }

  public static function accessB() {
    return 'Class B\'s Access';
  }

}

In other file:

use Folder\SubFolder\ClassA;

class ParentClass {

  public function __construct() {
    $this->access = new \Folder\Subfolder\ClassA;
  }

  public function admin() {
   $ac = $this->access::accessA(); 

   //When I do this it gives me an error syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)

   $ac = $this->access->accessA();

   //When I also do this it gives me an error Call to a member function accessA() on null

   var_dump($ac);
   exit;
  }
}

class ChildClass extends ParentsClass { 

 public function __construct() {
  parent::__construct();
 }

 public function employee() {
  $ac = $this->access::accessB();
  var_dump($ac);
  exit;
 }
}

class ChildClass2 extends ParentClass {
  public function __construct() {
   parent::__construct();
  }
} //And so on and so forth

NOTE: I don't want to instantiate another class object using new keyword. I want to use this code $this->access; only

Aucun commentaire:

Enregistrer un commentaire