doupuchao4256 2014-10-22 07:09
浏览 48
已采纳

无法将字符串转换为变量[关闭]

so i am pretty new to OOP PHP. i am trying to get a feel for it, i am makeing a simple class with 2 functions

one for speed of the car(he try's te see if the speed is a good value)

and one for color of the car(check if the color is vailid)

the error i got:

Catchable fatal error: Object of class Auto could not be converted to string in E:\Site peter+usbweboot\stage\oef\Auto.php on line 44

here is my code:

  <?php

class Auto {  


    public $mKleur = "";
    public $mSpeed = "";

    public function speed ($var){
        if(is_numeric($var)){
            if($var < 0 OR $var == 0)
            {

                $this->mSpeed = ("$var kmpu rijd die waggie niet eens?!");
            }
            elseif ($var > 206 OR $var == 206)
            {
                $$this->mSpeed = ("$var kmpu haalt de auto niet!");
            }
            elseif($var > 0 AND $var < 206)
            {
                    $$this->mSpeed = ("de auto reed met $var kmpu langs!");
            }
        }
            else
            {
                $$this->mSpeed = ( " $var dat niet eens een getal....");
            }

        }






    private $sKleur = "";  

    public function __set( $sAttribuut, $sValue ){ 
        switch( $sAttribuut ){ 
            case 'sKleur': 
                $aValideKleuren = array('groen', 'rood', 'blauw', 'paars', 'geel', 'wit', 'zwart','grijs','oranje' );      
                if( !in_array( strtolower( $sValue ), $aValideKleuren ) ){  
                   $$this->mKleur = ("Kleur niet bekend: $sValue");  

                }  
                else{
                    $$this->mKleur = ("kleur is bekent namelijk: $sValue");
                }
                $this->sKleur = $sValue; 
            break; 
            default: 
            break; 
        } 
    } 

    public function __get( $sAttribuut ){ 
        if( isset( $this->$sAttribuut ) ){ 
            return $this->$sAttribuut; 
        } 

    } 
     public function kk()
        {   
         $message = array('gSpeed' => $this->mSpeed, 'gKleur' => $this->mKleur);
        return($message);
        }



}   
?>

it does work if i use echo but i cant get it te return the value's

anyone know why?

  • 写回答

1条回答 默认 最新

  • duanjiaolia97750 2014-10-22 07:12
    关注

    You have a mistake. You put double $ before "this". Change $$this to $this

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

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?