[英]Passing variables from methods to methods inside a class
我需要使用數百個不同的通道來構建一個非常復雜的xml表。 為避免重復代碼並以6k行結尾,我編碼了兩種方法,一種創建無內容的通道,另一種創建填充的通道。 這是我的代碼,下面將解釋我的問題:
class create_xml {
private $xml_file;
private $title;
private $content;
private $date;
private $backlink;
private $filename = "feed.xml";
public function __construct($title, $content, $date, $backlink) {
$this->title = $title;
$this->content = $content;
$this->date = $date;
$this->backlink = $backlink;
$this->xml_file = new DOMDocument('1.0', 'utf-8');
}
//Creates a new channel without any content
private function cless_channel($chan_name) {
$$chan_name = $this->xml_file->createElement($chan_name);
$$chan_name = $this->xml_file->appendChild($$this->chan_name);
}
//Creates a populated channel
private function popu_channel($chan_name, $content, $parent) {
$sub_chan = $this->xml_file->createElement($chan_name);
$sub_chan = $$parent->appendChild($title);
$sub_content = $this->xml_file->createTextNode($content);
$sub_content = $sub_chan->appendChild($sub_content);
}
private function gen_xml() {
$this->cless_channel("channelroot");
$this->popu_channel("channel_one", "a random content", "channelroot");
}
public function save_xml() {
$this->gen_xml();
//Saves xml document
return $xml_file->save($filename);
}
public function return_xml() {
$this->gen_xml();
//Returns xml content as str
return $xml_file->saveXML();
}
}
好吧,基本上我會同時調用這兩個方法$ this-> cless_channel()和$ this-> popu_channel(),我正在使用動態變量將變量名從一種方法傳遞給另一種方法,但這顯然行不通。 我需要的是在類屬性和動態變量之間混合使用,以便能夠將變量從方法傳遞到其他方法。 請注意,這段代碼僅顯示了兩個節點,但是我將擁有更多節點,並且我認為聲明數量不多的變量是非常好的習慣,因此,我也在尋找一種避免這種情況的方法,但是我想我可以用數組做到這一點。 到目前為止,讓它開始工作將是一個很好的開始。 我嘗試了很多不同的東西,但是沒有一個起作用,所以我什至不去炫耀它們。
預先感謝您提供的任何幫助。
不確定我是否正確理解了這個問題,只是嘗試一下。
這個變量變量沒什么用。 可能會返回一個包含所有這些元素的數組,否則您將嘗試通過變量變量訪問這些元素。
或者,您可以改變該過程,然后將回調傳遞給skeleton方法,如果要向否則為空/默認元素的內容添加回調,它將“詢問”該回調。
但是,如果速度不是最高優先級,則只需讓創建空骨架的方法返回其創建的(最外層)元素,然后讓填充了某些(空)元素的方法用其他數據“抓住”這些元素,想要“更改/擴充/更改。
類似於:
<?php
$foo = new Foo;
$foo->skeleton('A');
$foo->populate1('B', 'lalala'); // this doesn't necessarily have to be in the same class
echo $foo->asxml();
class Foo {
private $root;
public function __construct() {
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$this->root = $doc->appendChild( $doc->createElement('root') );
}
public function skeleton($name) {
$container = $this->root->ownerDocument->createElement($name);
// for brevity adding raw xml to the container element
$fragment = $this->root->ownerDocument->createDocumentFragment();
$fragment->appendXML("<foo/><bar/><baz/><data><point /><point /><point /></data>");
$container->appendChild($fragment);
return $this->root->appendChild($container);
}
public function populate1($name, $point1) {
$container = $this->skeleton($name);
$xpath = new DOMXPath($container->ownerDocument);
foreach( $xpath->query('./data/point[1]', $container) as $p) { // can be only one, but anyway...
$p->appendChild( $container->ownerDocument->createTextNode($point1) );
}
}
public function asxml() {
return $this->root->ownerDocument->saveXML();
}
}
版畫
<?xml version="1.0"?>
<root>
<A>
<foo/>
<bar/>
<baz/>
<data>
<point/>
<point/>
<point/>
</data>
</A>
<B>
<foo/>
<bar/>
<baz/>
<data>
<point>lalala</point>
<point/>
<point/>
</data>
</B>
</root>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.