Static / non-static method issue

I am working on a simple ORM solution and faced a difficult situation. Ideally, I would like to use methods both in a static context and in the context of an object, depending on what it is called. I'm not sure if this is possible, but here is what I mean:

Say the user model wants to call where () statically, this currently works fine, for example:

$user = User::where('id = ?', 3);

Now I also maintain relationships, for example, the user may have messages. When this relationship is established, I simply save an empty copy of the message model in the user model and set the foreign key. For instance:

$user -> messages = new Message();
$user -> messages -> foreign_key = 'user_id';

Now, ideally, I would like to be able to call:

$user -> messages -> where('unread = ?', 1);

$this → foreign_key, , , . PHP? $this $this ( , , $this )

- ? , , static, , .

+5
4

, , Strict Standards, @drew010. , , , .

, , private static. __call() __callStatic(), . , " , ", - , $this , __call() . $this , .

, , , :

<?php

class test_class {

    private $instanceProperty = 'value';

    private static function where ($arg1, $arg2, $obj = NULL) {
        if (isset($obj)) {
            echo "I'm in an object context ($arg1, $arg2): I can access the instance variable: $obj->instanceProperty<br>\n";
        } else {
            echo "I'm in a static context ($arg1, $arg2)<br>\n";
        }
    }

    public function __call ($method, $args) {
        $method = "self::$method";
        if (is_callable($method)) {
            $args[] = $this;
            return call_user_func_array($method, $args);
        }
    }

    public static function __callStatic ($method, $args) {
        $method = "self::$method";
        if (is_callable($method)) {
            return call_user_func_array($method, $args);
        }
    }

}

test_class::where('unread = ?', 1);

$obj = new test_class();
$obj->where('unread = ?', 2);
+4

, PHP , .

, . , PHP , , , , - PHP 4, .

:

<?php

class Test {
    protected $_userId;

    public function find()
    {
        if (isset($this)) {
            echo "Not static.<br />\n";
        } else {
            echo "Static.<br />\n";
        }
    }
}

$t = new Test();
$t->find();

Test::find();

:

. .

:

.

: Test:: find() test.php 19
.

, , .

, , "", , , . , : public function find() public static function findStatic().

$obj->find(), Class::find(), , , . , , .

+4

, , , ... . , , .

$user->messages = new Message();

, .
$user->messages[] = new Message();?
, .

$user->messages->where('unread = ?', 1);

, .
, , , User: , :

$user->messages = Message::where('unread = ?', 1);

, where, :

$messages = Message::where(array(
    array('unread = ?', 1),
    array('id = ?',     $message->getID()),
));

: ORM - , - , Doctrine Propel.

+4

, , .

/ , , Injection, Service Container .

DI , , , , .

, , , , . / - - , ( ).

Don’t get me wrong → static methods have their purpose and advantages, however, the way you use it is partly outdated (although some structures, such as Laravel, promote this bad practice - for example, “Facades” and “Eloquent”).

0
source

All Articles