문제

I have trait ExportOptions with two static variables and two functions.

Variables are $Code and $ExportType. Content of $ExportType is set in function Set_ExportType. And both variables meet each other in function Convert_ExportCode.

Function Convert_ExportCode has following simple code:

private function Convert_ExportCode()
{
    switch(static::$ExportType)
    {
        /*
         * writes code
        */
        case UniT::UNIT_OPTION_END:
            self::$GlobalCode[] = $this -> LocalCode;
            $Text = preg_replace('/([\n]+)/', "\n", implode('', self::$GlobalCode));
            self::$GlobalCode = array();
            echo $Text;
            break;
        /*
         * exports code without writing
        */
        case UniT::UNIT_OPTION_STEP:
            self::$GlobalCode[] = $this -> LocalCode;
            $Text = preg_replace('/([\n]+)/', "\n", implode('', self::$GlobalCode));
            self::$GlobalCode = array();
            return $Text;
            break;
        /*
         * saves part of code
        */
        default:
            self::$GlobalCode[] = $this -> LocalCode;
            for($Order = 0; $Order < count(self::$GlobalCode); $Order++)
            {
                self::$GlobalCode[$Order] = preg_replace('/([\n]+)/', "\n", self::$GlobalCode[$Order]);
            }
    }
}

and it is (as whole trait, of course) used by three classes (CodeGenerator, SimpleAssembler, MenuAssembler_SelectOptgroup). It would be not problem, if those classes could not use each other.

Class CodeGenerator is used by both other classes - and class SimpleAssembler is used by class MenuAssembler_SelectOptgroup.

I know (because I tested it - and that class is on the first level) that CodeGenerator accepts and outputs (and handles) content of variable self::$Code as it should, even if it gets as text wrapped into any closed element code generated.

public function Execute()
{
    $CodePartNumber = count(self::$Code);

    /* some code that not handles with sel::$Code */

    // base of code storage into self::$Code
    self::$Code[$CodePartNumber] = $this -> Get_AssembledCode(/* arguments for vsprintf */);

    return $this -> Convert_ExportCode();
}

Class SimpleAssembler assembles and stores code in similar way:

public function Execute()
{
    $this -> Check_Orders();
    $CodePartNumber = count(self::$Code);               

    /*
     * generation of sub-level
     */
    for($Order = 0; $Order < count($this -> Content); $Order++)
    {
        $VMaX = new CodeGenerator($this -> Elements['sub']['main']);
        $VMaX -> Set_Text((empty($this -> Content[$Order]) ? '' : $this -> Content[$Order] ));

        if($Order < count($this -> Content)-1 )
        {
            $VMaX -> Set_ExportType(UniT::UNIT_OPTION_GOON);
            $VMaX -> Execute();
        }
        else
        {
            $VMaX -> Set_ExportType(UniT::UNIT_OPTION_STEP);
            self::$Code[$CodePartNumber] = $VMaX -> Execute();
        }


    }

    if($this -> Disable_TopLevel == FALSE)
    {
        /*
         * generation of top level element and inserting of columns into it
        */
        $VMaX = new CodeGenerator($this -> Elements['top']['main']);
        $VMaX -> Set_ExportType(static::$ExportType);
        $VMaX -> Set_Text(self::$Code[$CodePartNumber]);
        return $VMaX -> Execute();
    }
    else
    {
        return $this -> Convert_ExportCode();
    }
}

But class 'SimpleAssembler' has a problem with accepting of content of this variable. And in final output is

<ol >
    <ol >   
        <li >1</li>
        <li >2</li>
        <li >3</li>     
    </ol>
    <li >4</li>
    <li >5</li>
    <li >6</li>
</ol>

instead

<ol >
    <li >1</li>
    <li >2</li>
    <li >3</li>     
</ol>

<ol >
    <li >4</li>
    <li >5</li>
    <li >6</li> 
</ol>

I would like to know how to solve it.

Edit: I tried to use also non-static variable for in-class code storage between various operations - and give only final code to that static variable, and even if I added

$this -> LocalCode = NULL;

to constructor and destructor, result is still the same.

Edit: I improved code in the beginning of question to new version, as is currently.

도움이 되었습니까?

해결책

I was so close to solution ... and yesterday evening I finally found it.

Using of additional variable was right way. But I did a mistake because I did not separated code generated by various levels (various classes) fully. Because I have thought that each class using of that trait will have own static variable of the same name.

So, later I made split of originally simple array into array of more dimensions ... from

array([] => some code /* and so on */)

to

array([class][] => some code /* and so on */)

And also, I moved this array variable from original sharing between classes to separated class that I have already used for easier access to some options (some constants).

And also, I divorced original only one trait in two.

One trait has that static array typed variable used only by that help class - and function for handling of it.

Originally content of array was set outside function that returns or prints it (even after using of additional variable) - and inside this function was only decided if it will be returned or printed. Currently content of this array typed variable is set directly into this function, together with class name. This trait is used only by help class.

That other trait has that additional variable together with static variable that contains option for code export - and function for setting of this option. This trait is used by all classes that generate any code.

I am lokking for seeing any better solution.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top