dprfe04886 2012-10-10 01:55
浏览 30
已采纳

从父函数的父类调用函数

Let's say, for simplicity, this is how my page is set up:

class TFS extends PHP_db
{
    public function execute() {
        class Dostuff {
            public static function doit() {
                return "wee";
            }
        }
    Now here I can use Dostuff:doit() successfully
    And also $this->db->functionhere() which is from the PHP_db is also successful from here

    }
}

I need to figure how to call $this->db->functionhere() from within the doit() function inside the Dostuff class..

I have already tried this in the Dostuff class:

protected $parent_object;
public function __construct( $object ) {
    $this->parent_object = $object;
 }

and this from the execute() function: $dostuffclass = new Dostuff($this);

But then when I try to use $parent_object->db->functionhere() it doesn't work, telling me it's not an object.

  • 写回答

1条回答 默认 最新

  • dongpo8250 2012-10-10 02:07
    关注

    You could pass the $this context to the internal DoStuff class as a parameter, for example:

    class TFS extends PHP_db
    {
        public function execute() {
            class Dostuff {
                public static function doit($somevar) {
                    $somevar->functionhere();
                    ^^^^ <-- add this
                    return "wee";
                }
            }
        }
    }
    

    The internal class DoStuff does not need to know anything about the $this context you are referring to, since you are sending a reference to an existing class instance to it as a parameter.

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

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试