난 그냥 뭔가를 썼다는이 예는 전술의 핵심 드라이버 파일에 대한 추가 지원 패키지 (들)을해야 할 수 있습니다 사용자 정의 드라이버를 확장하는 기반으로. $ 아이에 대한 두 번째 위해서 var_dump()는 당신이 지금 말했다 특성이 앰퍼샌드 (&를) 앞에는 볼 수있는 점을 제외하고 $ 부모와 동일한 정보를 출력
// File: parent.php
class parent_class() {
protected $_protected_object; // set in constructor
protected $_protected_var = "Something dynamic";
public function __construct() {
// keep in mind that if you don't override this constructor, this will execute when extended
}
public function create_resource() {
$this->_protected_object = new SomeObjectInstance();
}
public function child_class() {
static $child = null;
if (is_null($child)) {
$file = "child.php";
if (!\file_exists($file)) {
throw new Exception("Couldn't load '$file', doesn't exist");
} else {
require_once($file);
$child = new child_class();
$child->__overload("_protected_object", $this->_protected_object)->__overload("_protected_var", $this->_protected_var);
}
}
return $child;
}
}
// File: child.php
class child_class extends parent_class {
protected function __overload($index, &$value) {
$this->$index =& $value;
return $this;
}
public function __construct() {
// REMEMBER: if you don't declare this method, the parent's constructor will execute
}
public function extended_func() {
// code
}
public function etc() {
// code
}
}
// Code instantiating objects:
$parent = new parent_class();
$parent->create_resource();
var_dump($parent);
/**
* Would output something like this:
*
* object(parent_class)#1 (2) {
* ["_protected_object":protected]=>
* object(SomeObjectInstance)#2 (*) {
* %OBJECT PROPERTIES%
* }
* ["_protected_var":protected]=>
* string(17) "Something dynamic"
* }
*/
$child = $parent->child_class();
var_dump($child);
/**
* Would output something like this:
*
* object(child_class)#3 (2) {
* ["_protected_object":protected]=>
* &object(SomeObjectInstance)#2 (*) {
* %OBJECT PROPERTIES%
* }
* ["_protected_var":protected]=>
* &string(17) "Something dynamic"
* }
*/
공지 사항, 지금은 참조가 있음을 나타내는. 따라서 두 속성과 관련하여 부모 클래스 인스턴스의 내용을 변경하면 자식 클래스 인스턴스에 반영됩니다.
Bar 인스턴스를 가져 와서 Foo 인스턴스를 채운 Foo 용 함수/생성자를 작성할 수 있습니까? – gunnx
왜 이것을 수행 하시겠습니까? 교활한 일이 일어나고있는 것처럼 들리네. –
PHP에서 사용자 지정 개체를 캐스팅 할 수 없다고 생각합니다. 아마도 [이 게시물] (http://stackoverflow.com/a/2232065/1028949)에서 유용한 정보를 찾을 수 있습니다. – Quantastical