Javascript like object literals. From http://www.phpied.com/javascript-style-object-literals-in-php/
Created
November 30, 2011 20:22
-
-
Save timw4mail/1410650 to your computer and use it in GitHub Desktop.
PHP Class for javascript-like objects
This file contains hidden or 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 | |
ob_start("ob_gzhandler"); | |
require('JSObject.php'); | |
$foo = new JSObject(array( | |
'bar' => array( | |
1, | |
2, | |
3 | |
), | |
'baz' => function($self) | |
{ | |
return $self->__toString('var_export'); | |
} | |
)); | |
$bar = new JSObject(array( | |
'bar' => array( | |
1, | |
2, | |
3 | |
), | |
'baz' => function($self) | |
{ | |
return $self->__toString('var_dump'); | |
} | |
)); | |
echo $foo->baz(); | |
echo $bar->baz(); | |
$foo->foo = $bar; | |
echo $foo; | |
echo (int)isset($foo->baz2); | |
echo $foo(array(1,2,3)); | |
echo $bar(array("a","b","c")); |
This file contains hidden or 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 | |
/** | |
* JSObject | |
* | |
* Class for creating object-literal-like contstructs in PHP | |
*/ | |
class JSObject { | |
/** | |
* Constructor for creating the objects | |
*/ | |
function __construct($members = array()) | |
{ | |
// Add the passed parameters to the object | |
foreach($members as $name => $value) | |
{ | |
$this->$name = $value; | |
} | |
} | |
// -------------------------------------------------------------------------- | |
/** | |
* PHP magic method to facilitate dynamic methods | |
* | |
* @param string $name | |
* @param array $args | |
*/ | |
function __call($name, $args) | |
{ | |
if(is_callable($this->$name)) | |
{ | |
//Add $this to the beginning of the args array | |
array_unshift($args, $this); | |
//Call the dynamic function | |
return call_user_func_array($this->$name, $args); | |
} | |
} | |
// -------------------------------------------------------------------------- | |
/** | |
* Prints out the contents of the object when used as a string | |
* | |
* @return string | |
*/ | |
function __toString() | |
{ | |
$args = func_get_args(); | |
$method = ( ! empty($args)) ? $args[0] : "print_r"; | |
$output = '<pre>'; | |
if($method == "var_dump") | |
{ | |
ob_start(); | |
var_dump($this); | |
$output .= ob_get_contents(); | |
ob_end_clean(); | |
} | |
else if($method == "var_export") | |
{ | |
ob_start(); | |
var_export($this); | |
$output .= ob_get_contents(); | |
ob_end_clean(); | |
} | |
else | |
{ | |
$output .= print_r($this, TRUE); | |
} | |
return $output . '</pre>'; | |
} | |
// -------------------------------------------------------------------------- | |
/** | |
* Constructor without the "new" | |
* | |
* @param array $members | |
*/ | |
function __invoke($members=array()) | |
{ | |
return new JSObject($members); | |
} | |
} | |
// End of JSObject.php |
@cowboy Doesn't recursively applying the class to arrays remove any arrays from the object, though? I got the code from somewhere else, I'm still toying around in my head as to what I would use it for.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd call them "JavaScript object-like" constructs and not "object-literal-like" constructs, as the behavior you're emulating isn't limited to JavaScript objects created via the object literal syntax, but in fact applies to any object in JavaScript. Also, shouldn't you also recursively call
JSObject
on any member that is also an array, so that nested "JavaScript object-like" arrays behave in the same way?