dongtanzhu5417 2010-11-10 17:19 采纳率: 100%
浏览 44
已采纳

class error当不在对象上下文中时使用$ this

I have the following class and its not accepting $this keyword in the method. can someone guid

<?php
class test {

    function __construct($x, $y, $z){

        $this->$x = $x;


        $this->testFunction();





public static function testFunction(){



    print '<br> here it is:'.$this->$x.'--<br>';
}



//========================================================================================================================
}
?>

it gives me this error

 Fatal error: Using $this when not in object context 
  • 写回答

3条回答 默认 最新

  • dongqiang8683 2010-11-10 17:21
    关注

    In a static function, you need to use self:

    print '<br> here it is:'.self::$x.'--<br>';
    

    $this refers to an object instance, which does not exist in a static context.

    That said, in a static context, the constructor will never have been called so $x will always be empty. I'm not sure whether public static function is really what you want here.

    Edit: Additionally, as @netcoder points out, $x needs to be declared a static member as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题