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 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作