これが XML に書き込まれ、$this->lastmatch[0] で関数呼び出しが見られないのはなぜですか?リターンは機能を終了しました

WBOY
リリース: 2016-06-20 12:52:11
オリジナル
1042 人が閲覧しました

class Conf{    private $file;    private $xml;    private $lastmatch;        function __construct($file){        $this->file = $file;        $this->xml =simplexml_load_file($file);    }        function write(){        file_put_contents($this->file,$this->xml->asXML());    }        function get($str){        $matches = $this->xml->xpath("/conf/item[@name=\"$str\"]");         if (count($matches)){           $this->lastmatch = $matches[0];           return (string) $matches[0];	       }    }        function set($key,$value){        if (!is_null($this->get($key))){        	  $this->lastmatch[0] = $value;  //这个为什么会写入xml,$this->lastmatch[0]没有看到函数调用?return已经结束函数返回了        	  return;        }        $conf = $this ->xml ->conf;        $this->xml->addChild('item',$value)->addAttribute('name',$key);    }}$conf = new Conf('conf.xml');$conf->set('user','test');$conf->write();
ログイン後にコピー

<?xml version="1.0" encoding="utf-8"?><conf>          <item name="user">bob</item>		  <item name="pass">newpass</item>	  <item name="host">localhost</item></conf>
ログイン後にコピー

これが XML に書き込まれているのに、$this->lastmatch[0] に関数呼び出しが見られないのはなぜですか? return 関数は終了しました


ディスカッションへの返信 (解決策)

書き込みはありません!
xml には
test
ノード


はありませんが、元のファイルは確かに変更されています。

ああ、間違った情報を提供したのですが、メイン投稿の最後の XML が結果だと思いましたか?

それではテストしてみましょう

    function get($str){echo $str; // user        $matches = $this->xml->xpath("/conf/item[@name=\"$str\"]");         if (count($matches)){           $this->lastmatch = $matches[0];print_r($matches[0]);/*userSimpleXMLElement Object(    [@attributes] => Array        (            [name] => user        ))*/           return (string) $matches[0];          }    }
ログイン後にコピー

if (!is_null( $this->get($key))){
$this->get($key) が String user を返しました
つまり、is_null は false を返します
そして、それを否定して true になります

条件式が true なので、分岐に入って代入演算を実行するのが自然です

章より17 19行目と19行目
$matches = $this->xml->xpath("/conf/item[@name="$str"]");
$this->lastmatch = $一致 [0];


$this->lastmatch[0] は $this->xml->xpath への参照です (xpath メソッドは SimpleXMLElement オブジェクトのコレクションを返します)

PHP の場合
変数 = オブジェクト
変数はオブジェクトへの参照であり、コピーではありません

わかりました、ありがとう

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!