Skip to content

Commit

Permalink
Fixed "Cannot access property ::$toStringFormat" when extending Carbo…
Browse files Browse the repository at this point in the history
…n and type juggling to a string occurs. Added supporting test.
  • Loading branch information
briannesbitt committed Nov 23, 2013
1 parent 64c0192 commit b0450b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
4 changes: 4 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.6.0 / 2013-11-23
==================
* Fixed "Cannot access property ::$toStringFormat" when extending Carbon and type juggling to a string occurs

1.5.0 / 2013-11-21
==================
* Diff for humans now shows 2 weeks ago instead of 14 days ago
Expand Down
28 changes: 14 additions & 14 deletions src/Carbon/Carbon.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Carbon extends DateTime
*
* @var array
*/
private static $days = array(
protected static $days = array(
self::SUNDAY => 'Sunday',
self::MONDAY => 'Monday',
self::TUESDAY => 'Tuesday',
Expand All @@ -78,7 +78,7 @@ class Carbon extends DateTime
*
* @var array
*/
private static $relativeKeywords = array(
protected static $relativeKeywords = array(
'this',
'next',
'last',
Expand Down Expand Up @@ -113,7 +113,7 @@ class Carbon extends DateTime
*
* @var string
*/
private static $toStringFormat = self::DEFAULT_TO_STRING_FORMAT;
protected static $toStringFormat = self::DEFAULT_TO_STRING_FORMAT;

/**
* A test Carbon instance to be returned when now instances are created
Expand Down Expand Up @@ -164,17 +164,17 @@ public function __construct($time = null, $tz = null)
// If the class has a test now set and we are trying to create a now()
// instance then override as required
if (static::hasTestNow() && (empty($time) || $time === 'now' || self::hasRelativeKeywords($time))) {
$testInstance = clone static::getTestNow();
if (self::hasRelativeKeywords($time)) {
$testInstance->modify($time);
}

//shift the time according to the given time zone
if ($tz !== NULL && $tz != static::getTestNow()->tz) {
$testInstance->setTimezone($tz);
} else {
$tz = $testInstance->tz;
}
$testInstance = clone static::getTestNow();
if (self::hasRelativeKeywords($time)) {
$testInstance->modify($time);
}

//shift the time according to the given time zone
if ($tz !== NULL && $tz != static::getTestNow()->tz) {
$testInstance->setTimezone($tz);
} else {
$tz = $testInstance->tz;
}

$time = $testInstance->toDateTimeString();
}
Expand Down
6 changes: 6 additions & 0 deletions tests/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use Carbon\Carbon;

class MyCarbon extends Carbon {}

class StringsTest extends TestFixture
{
public function testToString()
Expand All @@ -31,6 +33,10 @@ public function testResetToStringFormat()
Carbon::resetToStringFormat();
$this->assertSame($d->toDateTimeString(), ''.$d);
}
public function testExtendedClassToString() {
$d = MyCarbon::now();
$this->assertSame($d->toDateTimeString(), ''.$d);
}

public function testToDateString()
{
Expand Down

0 comments on commit b0450b5

Please sign in to comment.