まったく同じクラスインスタンスを含む可能性のある2つの変数を比較する方法がわかりません。PHPが変数がこの正確なクラスインスタンスであるかどうかを調べる
抽象クラス(その一部は以下に示されています)には、それを含むオブジェクトを識別するためのfetch_mother()メソッドがあり、スタックの最下部にあるため自身を返します。理論的には、スタックの深さは5以下でなければなりません。
ほとんどのインスタンスはカテゴリのようなものを表します。
期待出力された:[SITEURL] /システム/ドラフト/たとえばオン/また-DEV-ノート/
実際の出力した:SITEURL] /ゲットget_full_path()メソッドで
これは、サニティチェックがループを開始し、ループを壊すことを意味します。また、返されたオブジェクトが$this
と同じであることを正しくテストしていないことも意味します。
どうすれば確認できますか?$var===$this
?問題が起きる
コード:
<?php
namespace modules\content\classes;
use modules\core\interfaces as i;
use modules\core\classes as c;
abstract class content_object extends c\module_lib {
// vars
// ...
protected $mother;
protected $map
// ... code ...
public function get_object_map(){
return $this->map;
}
/**
* Get the stream holding this item
* @return \modules\content\classes\error|\modules\content\classes\content_object
*/
public function &fetch_mother(){
if(isset($this->mother) && is_object($this->mother)){
return $this->mother;
}
$mother = $this->module()->find_object_stream($this);
if(!($mother instanceof \modules\core\error) && is_object($mother) && $mother != $this){
$this->mother = $mother;
return $mother;
}else{
// I am my own mother ? \\
return $this;
}
}
protected function fetch_full_path_from_mother($path='',$sanity=10){
$map = $this->get_object_map();
$mother = $this->fetch_mother();
$path = $map . '/' . $path;
if($this==$mother || !is_object($mother) || $sanity<1){
return $path;
}
$sanity--;
return $mother->fetch_full_path_from_mother($path,$sanity);
}
public function get_full_path(){
$home = $this->get_core()->factory()->get_config('home');
return $home . $this->fetch_full_path_from_mother();
}
}
あなたの質問は不明ですか? '==='はあなたが望むものを正確に行い、オブジェクトが同じインスタンスであるかどうかをテストします。 – Twifty
それは私が思ったのだが... –
@Twifty nopsは彼が望むことをする。 '==='は、オブジェクトが同じ(まったく同じ)場合にのみtrueを返します。 – Lucas