简体   繁体   English

将变量从方法传递到类中的方法

[英]Passing variables from methods to methods inside a class

I need to build a very complex xml sheet with hundreds of different channels. 我需要使用数百个不同的通道来构建一个非常复杂的xml表。 To avoid repeating code and end up with 6k lines, I have coded two methods, one creating a contentless channel, and an other one creating a populated channel. 为避免重复代码并以6k行结尾,我编码了两种方法,一种创建无内容的通道,另一种创建填充的通道。 here is my code, I'll explain what my problem is below : 这是我的代码,下面将解释我的问题:

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();
    }
}

Alright so basicaly I'm calling both methods $this->cless_channel(), and $this->popu_channel(), I'm using dynamic variable to pass the variable name from one method to the other but that obviously does not work. 好吧,基本上我会同时调用这两个方法$ this-> cless_channel()和$ this-> popu_channel(),我正在使用动态变量将变量名从一种方法传递给另一种方法,但这显然行不通。 What I need is to mix between class attributes and dynamic variables to be able to pass variables from methods to other methods. 我需要的是在类属性和动态变量之间混合使用,以便能够将变量从方法传递到其他方法。 Note that this code only shows two nodes but I'll have many more and I don't think declaring as many variables as there will be channels is very good practice, so I'm also looking for a way to avoid that but I suppose I can do this with arrays. 请注意,这段代码仅显示了两个节点,但是我将拥有更多节点,并且我认为声明数量不多的变量是非常好的习惯,因此,我也在寻找一种避免这种情况的方法,但是我想我可以用数组做到这一点。 So far get this to work would be a great start. 到目前为止,让它开始工作将是一个很好的开始。 I have a tried a bunch of different things, none of them worked so I'm not even going to bother showing them off. 我尝试了很多不同的东西,但是没有一个起作用,所以我什至不去炫耀它们。

Thank you in advance for any sort of help you can provide. 预先感谢您提供的任何帮助。

Not quite sure if I understand the question correctly but just giving it a try. 不确定我是否正确理解了这个问题,只是尝试一下。
This variable variables thingy just won't work. 这个变量变量没什么用。 It would be possible to return an array that contains all those elements you would otherwise try to access through variable variables. 可能会返回一个包含所有这些元素的数组,否则您将尝试通过变量变量访问这些元素。
Or you could turn the process around and pass a callback to the skeleton method which would "ask" the callback if there's something to be added to the otherwise empty/default element. 或者,您可以改变该过程,然后将回调传递给skeleton方法,如果要向否则为空/默认元素的内容添加回调,它将“询问”该回调。
But if speed isn't the topmost priority just let the method that creates the empty skeleton return the (outermost) element it has created and let the method that populates some of the (empty) elements with additional data "grab" those elements it "wants to" alter/augment/change. 但是,如果速度不是最高优先级,则只需让创建空骨架的方法返回其创建的(最外层)元素,然后让填充了某些(空)元素的方法用其他数据“抓住”这些元素,想要“更改/扩充/更改。
Something along the line of: 类似于:

<?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();
    }
}

prints 版画

<?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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM