Reputation: 4592
Ok so my problem is trying to interface static and non-static methods, an example of both.
Regards.
Examples:
Codeigniter AR(non-static) and
php-activeRecord(static)
interface userRepoInterface
{
public function get($id=null); //should be static or non-static
}
class User extends ActiveRecord\Model implements userRepoInterface
{
public static function get ( $id = null )
{
try
{
if(is_null($id)){
$user = self::all();
}
elseif(is_int($id)){
$user = self::find($id);
}
}catch(ActiveRecord\RecordNotFound $e)
{
log_message('error', $e->getMessage());
return false;
}
return ($user) ?:$user;
}
}
class CIUser extends CI_Model implements userRepoInterface
{
public function __construct ()
{
parent::__construct () ;
}
public function get ( $id = null )
{
if ( is_null ( $id ) ) {
$user = $this->db->get('users');
}
elseif ( is_int($id) ) {
$user = $this->db->get_where('users', array('id'=> $id));
}
return ($user->num_rows() > 0) ? $user->result() : false;
}
}
-
class Authenticate
{
protected $userRepository;
public function __construct ( userRepoInterface $userRepository )
{
$this->userRepository = $userRepository;
print'<pre>';
print_r($this->userRepository->get());
}
public function __get ( $name )
{
$instance =&get_instance();
return $instance->$name;
}
}
-
public function index ()
{
$model1 = new \User; //this is static
$model2 = $this->load->model('CIUser'); //this is non-static
$this->load->library('Authenticate', $model1);
}
Upvotes: 0
Views: 386
Reputation: 1336
The only reasonable solution I see in this case is making it static in interface and something like this in classes where instance is to be used:
public static function get ( $id = null )
{
$instance = new static;
if ( is_null ( $id ) ) {
$user = $instance->db->get('users');
}
elseif ( is_int($id) ) {
$user = $instance->db->get_where('users', array('id'=> $id));
}
return ($user->num_rows() > 0) ? $user->result() : false;
}
Upvotes: 1