Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Another one

  • Loading branch information...
commit 4c8b25c118099065dd08cefdc3223cf05e9321ae 1 parent 44e086e
@chriso authored
Showing with 29 additions and 2 deletions.
  1. +6 −2 CallableProperties.php
  2. +23 −0 GettersSetters.php
View
8 CallableProperties.php
@@ -3,7 +3,9 @@
trait CallableProperties {
public function __call($property, $args) {
- if (!isset($this->$property) || !is_callable($this->$property)) {
+ if (!isset($this->$property)) {
+ return parent::__call($property, $args);
+ } elseif (!is_callable($this->$property)) {
$message = __CLASS__ . '::' . $property . ' is not callable.';
throw new BadMethodCallException($message);
}
@@ -11,7 +13,9 @@ public function __call($property, $args) {
}
public static function __callStatic($property, $args) {
- if (!isset(static::$property) || !is_callable(static::$property)) {
+ if (!isset(static::$property)) {
+ return parent::__callStatic($property, $args);
+ } elseif (!is_callable(static::$property)) {
$message = __CLASS__ . '::' . $property . ' is not callable.';
throw new BadMethodCallException($message);
}
View
23 GettersSetters.php
@@ -0,0 +1,23 @@
+<?php
+
+trait GettersSetters {
+
+ protected function tableize($property) {
+ return preg_replace_callback('/[A-Z]/', function ($char) {

Also add lcfirst ?

@chriso Owner
chriso added a note

Nm

Ah, ok, nvm... Just not my coding conventions, but as long as it's a protected function, I can live with it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ return '_' . $char;
+ }, $property);
+ }
+
+ public function __call($method, $args) {
+ $property = $this->tableize(substr($method, 3));
+ switch (substr($method, 0, 3)) {
+ case 'get':
+ return isset($this->$property) ? $this->$property : null;
+ case 'set':
+ return $this->$property = isset($args[0]) ? $args[0] : null;
+ case 'has':
+ return isset($this->$property);
+ }
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.