开发者

how to determine state of DB_DataObject instance?

As I add methods to various DB_DataObject classes, I lean towards

Class Example extends DB_DataObject {
function methodx() //start fresh
{
    $newObject = DB_DataObject::factory($this->__table);开发者_运维问答
    /* do stuff with $newObject */
}

rather than

function methodx() //use current instance
{
    /* do stuff with $this */
}

I've realized I do this because I don't know how to determine the state of $this. The calling code might have done any of the following:

$e = DB_DataObject::factory('Example'); $e->get(16); $e->methodx();
$e = DB_DataObject::factory('Example'); $e->somekey=$value; $e->methodx();
$e = DB_DataObject::factory('Example'); $e->somekey=$value; $e->find(); $e->methodx();

How can I determine the state of "$this" when I get my hands on it?

Are there rules of thumb or design for using the current instance vs. creating a new instance of the class?


If your method requires a "clean state" (class variables set to their default values), then simply create a new instance.

All you would save is a object instantiation, which is cheap.


You could determine if the state changed by using [ReflectionClass::getDefaultProperties()][1] and comparing them with the current property values. And then you'd have to check if any new properties have been added dynamically.

Alternatively, create a new instance and compare all properties with the current one :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜