无法从PHP类函数获取返回值

时间:2016-10-04 01:54:25

标签: javascript php

我使用在线转换器将http://7xl.org/_msg/mugenzaru的Javascript *转换为PHP。代码有效,但我不知道如何使函数$ mugenzaru-> doIt返回变量$ resultString。有什么想法吗?

  • aЈapanеseⅬоremІpsumgеnerator

原始Javascript:http://7xl.org/_msg/mugenzaru/js.htm

生成并略微改编的PHP:http://7xl.org/_msg/mugenzaru/php.htm

2 个答案:

答案 0 :(得分:1)

将所有字符串附加内容从+=更改为.=

答案 1 :(得分:1)

“等效”php函数的工作方式存在差异,因此存在问题,特别是pickOne()计算。 Math.random()random()的工作方式不同,所以数字很远。看看这是否让你更接近。有一点需要注意,日文字符会让SO认为它的垃圾邮件,所以我把它们排除在外但是它们确实需要填充这些数组:

class   Characters
    {   
        private $mugenzaru;

        public  function getString(Mugenzaru $mugenzaru,$charType = 'kangi')
            {
                $this->mugenzaru    =   $mugenzaru;
                $kanji              =   array(2, 2, 2, 2, 2, 2, 1, 1, 1, 3, 3, 4);
                $hiragana           =   array(1, 1, 1, 1, 2, 2, 2, 3, 3, 4);
                $katakana           =   array(2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 6, 7);
                $punctuation        =   array(1);
                $lengthWeighting    =   ${$charType};
                $charStringLength   =   $this->mugenzaru->eenieMeenie()->pickOne($lengthWeighting);
                $resultString       =   "";
                while ($charStringLength != 0) {
                    $resultString .= $this->mugenzaru->eenieMeenie()->pickOne($this->mugenzaru->{$charType}());
                    $charStringLength--;
                }
                return $resultString;
            }
    }

class   EenieMeenie 
    {
        public  function pickOne($characterList)
            {
                $rand   =   "0.".str_pad(mt_rand(1,9999999999999999),16,0,STR_PAD_LEFT);
                $var    =   floor($rand*count($characterList));
                return $characterList[$var];
            }
    }

class Mugenzaru
    {
        private $length =   10;

        public function eenieMeenie()
            {
                return new EenieMeenie();
            }

        public  function __call($name, $args = false)
            {
                $kanji      =   array(/*** array of kanji ***/);
                $hiragana   =   array(/*** array of hiragana ***/);
                $katakana   =   array(/*** array of katakana ***/);
                $punctuation    =   array(/*** array of punctuation ***/);

                return ${$name};
            }

        public  function setLength($num = 1000)
            {
                $this->length   =   $num;
                return $this;
            }

        public  function doIt(Characters $char) {
                $weighting = array(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 4, 4);
                $resultString = "";
                $targetLength = $this->length;

                while(strlen($resultString) < $targetLength) {
                    $charWeight =   $this->eenieMeenie()->pickOne($weighting);
                    switch ($charWeight){
                        case 1:
                            $resultString .= $char->getString($this,'kanji');
                            break;
                        case 2:
                            $resultString .= $char->getString($this,'hiragana');
                            break;
                        case 3:
                            $resultString .= $char->getString($this,'katakana');
                            break;
                        case 4:
                            $resultString .= $char->getString($this,'punctuation');
                            break;
                        default:
                            $resultString .= $char->getString($this,'kanji');
                    }
                }

                return $resultString;
            }
    }


$Mugenzaru  =    new Mugenzaru();
echo $Mugenzaru->setLength(1000)->doIt(new Characters);