这是我正在运行以了解框架如何工作的测试。我编写了一个简单的外观实现,一个服务提供者,并且我正确地注册了它。代码:
// 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()
我认为这与魔术方法有关。我试图静态调用它们,但没有任何变化。我该如何调用
一个()
来自
相比()
身体?
== 编辑 ==
我正在写一个针对这个门面的测试,就像这样
<?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 致命错误:在第 15 行的 /var/www/my/app/my/utils/DateHelper.php 中调用未定义的方法 DateHelperTest::parseInput()
那么,这是什么?DateHelper 中的 $this->fooBar 方法被解析为 DateHelperTest->foobar()?