Laravel4,Facade实现中的访问实例方法最终导致PHP致命:调用未定义的方法

时间:2013-09-13 14:48:45

标签: php laravel laravel-4

这是我正在运行的测试,以了解框架的工作原理。我编写了一个简单的门面实现,一个服务提供商,我正确地注册了它。代码:

// This is the facade 
<?php
namespace my\facades;
use Illuminate\Support\Facades\Facade;

class DateHelper extends Facade {
    /**
     *  Get the registered name of the component.
     *  @return string
     */
    protected static function getFacadeAccessor() { 
        // Return the instance key name in the IoC Container.
        return 'my.utils.dateHelper'; 
    }

}

// This is the service provider
<?php
namespace my\providers;
class MyServiceProvider extends \Illuminate\Support\ServiceProvider {
    /**
     *  Register the service provider.
     */
    public function register() {

    // Register the date handler
    $this->app['my.utils.dateHelper'] = $this->app->share(function($app) {
            return new \my\utils\DateHelper();
    });



        // Shortcut so developers don't need to add an Alias in app/config/app.php
        $this->app->booting(function(){
            $loader = \Illuminate\Foundation\AliasLoader::getInstance();

            $loader->alias('DateHelper', '\my\utils\DateHelper');

        });
    }
}
// This is the actual implementation
class DateHelper {
    public static $UI_DATE_FORMAT = 'd/m/y';
    public static $UI_DATETIME_FORMAT = 'd/m/y H:i:s';

    /**
     * 
     *  Enter description here ...
     *  @param DateTime $date0
     *  @param DateTime $date1
     */
    public function compare(mixed $date0, mixed $date1) {
        if (is_string($date0)) {
            $date0 = $this->a($date0)->getTimestamp();
        }
        if (is_string($date1)) {
            $date1 = $this->a($date1)->getTimestamp();
        }
        return $date0 < $date1 ? -1 : $date1 > $date0 ? 1 : 0;
    }

    /**
     * 
     *  Enter description here ...
     *  @param string $inputDate
     */
    public function a( $inputDate) {
        return \DateTime::createFromFormat(self::$UI_DATE_FORMAT, $inputDate);
    }
}

现在,即从控制器,我能够

  • 调用DateHelper :: compare(“one”,“two”);
  • 调用DateHelper :: a(1);

方法得到妥善解决。什么是无效的是排序

$date0 = $this->a($date0)->getTimestamp();

我得到一个PHP致命的陈述

PHP Fatal error:  Call to undefined method DateHelperTest::a()

我认为这与魔术方法有关。我试着调用它们 静静地,但没有任何变化。我怎么能调用

  

A()

来自

  

比较()

体?

==编辑==

我正在写这个门面的测试,就像这个

<?php
class DateHelperTest extends \TestCase {

    public function testComparison() {
        // Compare strings
        $d0 = "01/01/2012";
        $d1 = "02/01/2012";
        $this->assertEquals(DateHelper::compare($d0, $d1), 1);
        $this->assertEquals(DateHelper::compare($d1, $d0), -1);
        $this->assertEquals(DateHelper::compare($d0, $d0), 0);

        // Compare DateTimes
        $dateTime0 = new DateTime($d0);
        $dateTime1 = new DateTime($d1);
        $this->assertEquals(DateHelper::compare($dateTime0, $dateTime1), 1);
        $this->assertEquals(DateHelper::compare($dateTime1, $$dateTime0), -1);
        $this->assertEquals(DateHelper::compare($dateTime0, $dateTime1), 0);
  }

  public function testParser() {
    $sample = "01/02/2012";
    $d = DateHelper::parseInput($sample);
    $this->assertEquals($d->format("d/m/Y"), $sample);
    $this->assertEquals($d->format("d"), "01");
    $this->assertEquals($d->format("m"), "02");
    $this->assertEquals($d->format("Y"), "2012");
  }

}

我刚刚发现了一个我最初没有看到的奇怪的事情。错误说明

  

PHP致命错误:调用未定义的方法   DateHelperTest :: parseInput()in   第15行的/var/www/my/app/my/utils/DateHelper.php

所以,这是什么? DateHelper中的$ this-&gt; fooBar方法被解析为DateHelperTest-&gt; foobar()?

1 个答案:

答案 0 :(得分:2)

我以前没有看到过以这种方式使用的AliasLoader,但我认为你的问题可能存在。别名应该指向Facade,而不是实际的类。

(我还鼓励你重新命名你的门面,以便更清楚哪个级别是门面,哪个是基础课程 - 我打赌你至少要烧掉自己一旦使用了错误的命名空间并获得了一个你不期望的课程。)