dongxiaoying5882 2016-09-29 16:43
浏览 21
已采纳

无法将单词Convert的对象转换为单词中的字符串数量

i used this code to call a function that will convert my number into words words but when i added it on database it gave me error like this : Object of class Convert could not be converted to string did i do wrong thanks for help.

class Convert {
    var $words = array();
    var $places = array();
    var $amount_in_words;
    var $decimal;
    var $decimal_len;

    function __construct($amount, $currency = "Pesos") {
        $this->assign();

        $temp = (string)$amount;
        $pos = strpos($temp,".");
        if ($pos) {
            $temp = substr($temp,0,$pos);
            $this->decimal = strstr((string)$amount,".");
            $this->decimal_len = strlen($this->decimal) - 2;
            $this->decimal = substr($this->decimal,1,$this->decimal_len+1);
        }
        $len = strlen($temp)-1;
        $ctr = 0;
        $arr = array();
        while ($len >= 0) {
            if ($len >= 2) {
                $arr[$ctr++] = substr($temp, $len-2, 3);
                $len -= 3;
            } else {
                $arr[$ctr++] = substr($temp,0,$len+1);
                $len = -1;
            }
        }

        $str = "";
        for ($i=count($arr)-1; $i>=0; $i--) {
            $figure = $arr[$i];
            $sub = array(); $temp="";
            for ($y=0; $y<strlen(trim($figure)); $y++) {
                $sub[$y] = substr($figure,$y,1);
            }
            $len = count($sub);
            if ($len==3) {
                if ($sub[0]!="0") {
                    $temp .= ((strlen($str)>0)?" ":"") . trim($this->words[$sub[0]]) . " Hundred";
                }
                $temp .= $this->processTen($sub[1], $sub[2]);
            } elseif ($len==2) {
                $temp .= $this->processTen($sub[0], $sub[1]);
            } else {
                $temp .= $words[$sub[0]];
            }
            if (strlen($temp)>0) {
                $str .= $temp . $this->places[$i];
            }
        }
        $str .= " " . $currency;
        if ($this->decimal_len>0) {
            $str .= " And " . $this->decimal .  " Cents";
        }
        $this->amount_in_words = $str;
    }

    function denominator($x) {
        $temp = "1";
        for ($i=1; $i<=$x; $i++) {
            $temp .= "0";
        }
        return $temp;
    }

    function display() {
        echo $this->amount_in_words;
    }

    function processTen($sub1, $sub2) {
        if ($sub1=="0") {
            if ($sub2=="0") {
                return "";
            } else {
                return $this->words[$sub2];
            }
        } elseif ($sub1!="1") {
            if ($sub2!="0") {
                return $this->words[$sub1."0"] . $this->words[$sub2];
            } else {
                return $this->words[$sub1 . $sub2];
            }
        } else {
            if ($sub2=="0") {
                return $this->words["10"];
            } else {
                return $this->words[$sub1 . $sub2];
            }
        }
    }

    function assign() {
        $this->words["1"] = " One";             $this->words["2"] = " Two";
        $this->words["3"] = " Three";       $this->words["4"] = " Four";
        $this->words["5"] = " Five";            $this->words["6"] = " Six";
        $this->words["7"] = " Seven";           $this->words["8"] = " Eight";
        $this->words["9"] = " Nine";

        $this->words["10"] = " Ten";            $this->words["11"] = " Eleven";
        $this->words["12"] = " Twelve";     $this->words["13"] = " Thirten";
        $this->words["14"] = " Fourten";        $this->words["15"] = " Fiften";
        $this->words["16"] = " Sixten";     $this->words["17"] = " Seventen";
        $this->words["18"] = " Eighten";        $this->words["19"] = " Nineten";

        $this->words["20"] = " Twenty";     $this->words["30"] = " Thirty";
        $this->words["40"] = " Forty";      $this->words["50"] = " Fifty";
        $this->words["60"] = " Sixty";      $this->words["70"] = " Seventy";
        $this->words["80"] = " Eighty";     $this->words["90"] = " Ninety";

        $this->places[0] = "";                  $this->places[1] = " Thousand";
        $this->places[2] = " Million";      $this->places[3] = " Billion";
        $this->places[4] = " Thrillion";
    }
}






$convert = new Convert($Amount);
            $convert->display(); echo " Only";
            $sql = "INSERT INTO vouchercheck (Payee,CheckNo,Date1,AmountinWord,Amount) VALUES ('$Payee','$CheckNo','$Date','".$convert."','$Amount')";
  • 写回答

1条回答 默认 最新

  • dpbrrczhlwbv849228 2016-09-29 16:49
    关注

    We need the code for your Convert class.

    With that said, the obvious error being returned is that you are trying to concatenate an "object" (your $convert variable is an object of the Convert class) with a string.

    Your Convert class should have a method like "display" that returns the "AmountinWord" string you are expecting.

    Display might be that class method, although you should NOT be using "echo" in that method, but rather you should be using return $somevalue;

    In that case you would want:

    $sql = "INSERT INTO vouchercheck (Payee,CheckNo,Date1,AmountinWord,Amount) VALUES ('$Payee','$CheckNo','$Date','" . $convert->display() . "','$Amount')";
    

    An alternative method to use in your Convert class, is to define the "magic method" __toString. This comes from the Java world, where classes often have a toString method defined that will render a string compatible version of an object. So in your class you would want something like:

    public function __toString()
    {
        return $this->amount_in_words;
    }
    

    If you have this method, then you would be able to use the existing code you had:

    $sql = "INSERT INTO vouchercheck (Payee,CheckNo,Date1,AmountinWord,Amount) VALUES ('$Payee','$CheckNo','$Date','".$convert."','$Amount')";
    

    Full Class code will now be:

    class Convert { var $words = array(); var $places = array(); var $amount_in_words; var $decimal; var $decimal_len;

    function __construct($amount, $currency = "Pesos") {
        $this->assign();
    
        $temp = (string)$amount;
        $pos = strpos($temp,".");
        if ($pos) {
            $temp = substr($temp,0,$pos);
            $this->decimal = strstr((string)$amount,".");
            $this->decimal_len = strlen($this->decimal) - 2;
            $this->decimal = substr($this->decimal,1,$this->decimal_len+1);
        }
        $len = strlen($temp)-1;
        $ctr = 0;
        $arr = array();
        while ($len >= 0) {
            if ($len >= 2) {
                $arr[$ctr++] = substr($temp, $len-2, 3);
                $len -= 3;
            } else {
                $arr[$ctr++] = substr($temp,0,$len+1);
                $len = -1;
            }
        }
    
        $str = "";
        for ($i=count($arr)-1; $i>=0; $i--) {
            $figure = $arr[$i];
            $sub = array(); $temp="";
            for ($y=0; $y<strlen(trim($figure)); $y++) {
                $sub[$y] = substr($figure,$y,1);
            }
            $len = count($sub);
            if ($len==3) {
                if ($sub[0]!="0") {
                    $temp .= ((strlen($str)>0)?" ":"") . trim($this->words[$sub[0]]) . " Hundred";
                }
                $temp .= $this->processTen($sub[1], $sub[2]);
            } elseif ($len==2) {
                $temp .= $this->processTen($sub[0], $sub[1]);
            } else {
                $temp .= $words[$sub[0]];
            }
            if (strlen($temp)>0) {
                $str .= $temp . $this->places[$i];
            }
        }
        $str .= " " . $currency;
        if ($this->decimal_len>0) {
            $str .= " And " . $this->decimal .  " Cents";
        }
        $this->amount_in_words = $str;
    }
    
    function __toString() {
        return $this->amount_in_words;
    }
    
    function denominator($x) {
        $temp = "1";
        for ($i=1; $i<=$x; $i++) {
            $temp .= "0";
        }
        return $temp;
    }
    
    function display() {
        echo $this->amount_in_words;
    }
    
    function processTen($sub1, $sub2) {
        if ($sub1=="0") {
            if ($sub2=="0") {
                return "";
            } else {
                return $this->words[$sub2];
            }
        } elseif ($sub1!="1") {
            if ($sub2!="0") {
                return $this->words[$sub1."0"] . $this->words[$sub2];
            } else {
                return $this->words[$sub1 . $sub2];
            }
        } else {
            if ($sub2=="0") {
                return $this->words["10"];
            } else {
                return $this->words[$sub1 . $sub2];
            }
        }
    }
    
    function assign() {
        $this->words["1"] = " One";             $this->words["2"] = " Two";
        $this->words["3"] = " Three";       $this->words["4"] = " Four";
        $this->words["5"] = " Five";            $this->words["6"] = " Six";
        $this->words["7"] = " Seven";           $this->words["8"] = " Eight";
        $this->words["9"] = " Nine";
    
        $this->words["10"] = " Ten";            $this->words["11"] = " Eleven";
        $this->words["12"] = " Twelve";     $this->words["13"] = " Thirten";
        $this->words["14"] = " Fourten";        $this->words["15"] = " Fiften";
        $this->words["16"] = " Sixten";     $this->words["17"] = " Seventen";
        $this->words["18"] = " Eighten";        $this->words["19"] = " Nineten";
    
        $this->words["20"] = " Twenty";     $this->words["30"] = " Thirty";
        $this->words["40"] = " Forty";      $this->words["50"] = " Fifty";
        $this->words["60"] = " Sixty";      $this->words["70"] = " Seventy";
        $this->words["80"] = " Eighty";     $this->words["90"] = " Ninety";
    
        $this->places[0] = "";                  $this->places[1] = " Thousand";
        $this->places[2] = " Million";      $this->places[3] = " Billion";
        $this->places[4] = " Thrillion";
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?