Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 接口静态和非静态方法_Php_Codeigniter_Interface_Static_Dependency Injection - Fatal编程技术网

Php 接口静态和非静态方法

Php 接口静态和非静态方法,php,codeigniter,interface,static,dependency-injection,Php,Codeigniter,Interface,Static,Dependency Injection,好的,我的问题是尝试连接静态和非静态方法,这两者都是一个例子 问候 示例: 代码点火器AR(非静态)和 php activeRecord(静态) 接口 Php活动记录 共点火器 - 测试用例库(验证) 在这种情况下,我看到的唯一合理的解决方案是在接口中使其成为静态的,在要使用实例的类中也是这样: 我不认为混合使用静态和非静态的方法是一个好主意?我只是想把我的身份验证库解耦。它们都使用相同的接口,所以它的接口声明方法是静态的还是非静态的,所有的实现者都必须遵循它。这正是问题所在。 interfac

好的,我的问题是尝试连接静态和非静态方法,这两者都是一个例子

问候

示例:

代码点火器AR(非静态)和

php activeRecord(静态)

接口 Php活动记录 共点火器 -

测试用例库(验证)
在这种情况下,我看到的唯一合理的解决方案是在接口中使其成为静态的,在要使用实例的类中也是这样:


我不认为混合使用静态和非静态的方法是一个好主意?我只是想把我的身份验证库解耦。它们都使用相同的接口,所以它的接口声明方法是静态的还是非静态的,所有的实现者都必须遵循它。这正是问题所在。
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);

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