Last active
December 30, 2015 09:19
-
-
Save olvlvl/7808480 to your computer and use it in GitHub Desktop.
I wanted to create traits for properties such as `created_at` and `updated_at` for my ActiveRecord implementation, but it looks like even if the methods are aliased, PHP consider that there is a clash and throw a fatal error. That makes me sad. Is there a solution to that ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ICanBoogie\ActiveRecord; | |
/** | |
* Defines the `created_at` property and the corresponding getter/setter. | |
* | |
* @property \ICanBoogie\DateTime $created_at The date and time at which the record was created. | |
*/ | |
trait CreatedAtProperty | |
{ | |
use DateTimeProperty | |
{ | |
DateTimeProperty::set_datetime as set_created_at; | |
DateTimeProperty::get_datetime as get_created_at; | |
} | |
private $created_at; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ICanBoogie\ActiveRecord; | |
use ICanBoogie\DateTime; | |
trait DateTimeProperty | |
{ | |
/** | |
* Sets the date and time. | |
* | |
* @param string|\DateTime $value | |
*/ | |
protected function set_datetime($value) | |
{ | |
# | |
# debug_backtrace is used to retrieve the name of the property because we can't retrieve | |
# the name of the function when it's aliased by trait. | |
# | |
$backtrace = debug_backtrace(0, 2); | |
$property = $backtrace[1]['args'][0]; | |
$this->$property = $value; | |
} | |
/** | |
* Returns the date and time represented by a {@link \ICanBoogie\DateTime} instance. | |
* | |
* @return \ICanBoogie\DateTime A {@link \ICanBoogie\DateTime} instance. | |
*/ | |
protected function get_datetime() | |
{ | |
# | |
# debug_backtrace is used to retrieve the name of the property because we can't retrieve | |
# the name of the function when it's aliased by trait. | |
# | |
$backtrace = debug_backtrace(0, 2); | |
$property = $backtrace[1]['args'][0]; | |
$datetime = $this->$property; | |
if ($datetime instanceof DateTime) | |
{ | |
return $datetime; | |
} | |
return $this->$property = ($datetime === null) ? DateTime::none() : new DateTime($datetime, 'utc'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use ICanBoogie\ActiveRecord\DateTimeProperty; | |
class Node extends \ICanBoogie\ActiveRecord | |
{ | |
use \ICanBoogie\ActiveRecord\CreatedAtProperty, | |
\ICanBoogie\ActiveRecord\ModifiedAtProperty | |
{ | |
DateTimeProperty::get_datetime insteadof CreatedAtProperty; | |
DateTimeProperty::get_datetime insteadof CreatedAtProperty; | |
DateTimeProperty::set_datetime insteadof ModifiedAtProperty; | |
DateTimeProperty::set_datetime insteadof ModifiedAtProperty; | |
} | |
} | |
// ok ! but that's so ugly now ! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ICanBoogie\ActiveRecord; | |
/** | |
* Defines the `updated_at` property and the corresponding getter/setter. | |
* | |
* @property \ICanBoogie\DateTime $updated_at The date and time at which the record was updated. | |
*/ | |
trait UpdatedAtProperty | |
{ | |
use DateTimeProperty | |
{ | |
DateTimeProperty::set_datetime as set_updated_at; | |
DateTimeProperty::get_datetime as get_updated_at; | |
} | |
private $updated_at; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The correct file order is "datetime.php", "created_at.php", "update_at.php", and "index.php".