dreinuqm992401 2014-04-01 01:31
浏览 52
已采纳

使用命名空间在扩展类中访问$ this

B.php:

class B extends A {
    function foo() {
        $this->get['id'];
    }
}

C.php:

namespace NAME;
class C extends B {
    function foo() {
        $b = new \B();
        $b->get['id'] = $params['userToken'];
    }
}

The above works but how to I access $this instead of creating a new instance of B?

If I try and access it like this:

namespace NAME;
class C extends B {
    function foo() {
        $this->get['id'] = $params['userToken'];
    }
}

It returns the error: Using $this when not in object context. $this is protected but as it C is extending B it shouldn't cause an issue and if it does, shouldn't return that error.

  • 写回答

1条回答 默认 最新

  • dragon0118 2014-04-01 02:01
    关注

    Your last piece of code will work, but when you use call_user_func() to call a method that uses $this you need to pass an instance instead of the class name, i.e.

    call_user_func([new C, 'foo']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作