我有testClass
类,我想在其构造函数中从未序列化的对象创建新对象。可能吗?像这样:
class testClass {
public __constructor($id) {
$queryData = DB::query(sql);
$object = unserialize($queryData);
$this = $object;
}
}
通过这种方式,代码的其余部分将不会打扰对象的构造方式:
$object = new testClass(3);
答案 0 :(得分:0)
unserialize
应该已经为您提供了对象。
$object = new TestClass();
$queryData = serialize($object);
$object2 = unserialize($queryData);
// object2 is now a copy of object
答案 1 :(得分:0)
建议Mark Baker
和Frits van Campen
,我使用了工厂:
public static function factory($idText = NULL, $rawText = NULL, $properties = NULL) {
if ($idText === NULL) {
return new Article($idText, $rawText, $properties);
} else {
$fetchedObject = self::fetchStoredObject($idText);
if ($fetchedObject !== NULL) {
return $fetchedObject;
} else {
throw new Exception('Article with ID ' . $idText . ' does not exist');
}
}
}