Using call_user_func with method_exists - php

Try to call method_exists on method registred with call_user_func.
<?php
class stdClass1
{
public static $methods = [];
public function __call($method, $arguments) {
return call_user_func_array(Closure::bind(self::$methods[$method], $this, get_called_class()), $arguments);
}
public function __set($name, $value) {
if (is_callable($value)) {
self::$methods[$name] = $value;
} else {
parent::__set($name, $value);
}
}
}
class stdClass2
{
function stdRunMethod()
{
$obj = new stdClass1();
$obj->test = function () {
echo 'a simple function'.PHP_EOL;
};
var_dump(method_exists($obj, "test"));
}
}
$obj = new stdClass2();
$obj->stdRunMethod();
method_exists return false. How to check this method with method_exists? Why method_exists return false?

Because test is not a method. It's a property which stores anonymous function.
If you want to check if value of property can be called as function you can use is_callable:
var_dump(is_callable([$obj, "test"]));

Related

PHP [closures] [override method runtime] How Improve this wrapp

I wrote this code for override a object method:
class A{
public function foo(){
echo "foo";
}
}
$a = new A();
$a->foo = function(){
echo "don't work";
}
$a->foo();
but the above code doesn't work i tried use a Closure but it didn't work.
so i wrote this utility class in my project, the class work and it works as expected.
class Wrapper
{
public $wrapped;
private $binds;
private $obj;
public function __construct($obj)
{
$this->wrapped = true;
$this->obj = $obj;
$this->binds = [];
}
public function bind($name, $function){
$this->binds[$name] = \Closure::bind($function, $this->obj, get_class($this->obj));
}
public function __set($name, $value)
{
$this->obj->$name = $value;
}
public function __get($name)
{
if(property_exists($this, $name)){
return $this->$name;
}
return $this->obj->$name;
}
public function __isset($name)
{
return isset($this->obj->$name);
}
public function __unset($name)
{
unset($this->obj->$name);
}
public function __call($name, $arguments)
{
if (array_key_exists($name, $this->binds) ) {
return call_user_func_array($this->binds[$name], $arguments);
}
return call_user_func_array([$this->obj, $name], $arguments);
}
public static function __callStatic($name, $arguments)
{
return call_user_func_array([get_class($this->obj, $name), $name], $arguments);
}
}
but the above class is a dirty fix, so how i can use the closure class or other method for do the same thing?

php dynamically append method to class

Need dynamically append method to class.
My code:
<?php
class stdClass1 {
public function __call($method, $arguments) {
return call_user_func_array(Closure::bind($this->$method, $this, get_called_class()), $arguments);
}
}
class stdClass2 {
function stdRunMethod() {
$obj = new stdClass1();
$obj->test = function() {
echo 'a simple function';
};
$obj->test();
$obj2 = new stdClass1();
$obj2->test();
}
}
$obj = new stdClass2();
$obj->stdRunMethod();
Question: why test method run only for first instance of stdClass1 class? How to append this method for all new instances?
try this instead (demo):
<?php
class stdClass1 extends \stdClass
{
private static $addedClosures = array();
public function __set($name, $value)
{
if ($value instanceof \Closure) {
self::$addedClosures[$name] = $value;
}
else {
parent::__set($name, $value);
}
}
public function __call($method, $arguments)
{
if (isset(self::$addedClosures[$method]))
return call_user_func_array(self::$addedClosures[$method], $arguments);
return call_user_func_array($method, $arguments);
}
}
class stdClass2 extends \stdClass
{
function stdRunMethod()
{
$obj = new stdClass1();
$obj->test = function () {
print_r('a simple function');
};
$obj->test();
$obj2 = new stdClass1();
$obj2->test();
}
}
The reason it only runs once is that each copy of stdClass1 maintains their own set of variables. In the following
$obj1 = new stdClass1();
$obj1->a = '1';
$obj2 = new stdClass1();
$obj2->a = '2';
echo $obj1->a;
You'll get the value 1 as the output. Because in most cases they maintain different references. Unless you use the static keyword. Static properties are shared between all instances of the class, and should be used carefully, but that's what you're thinking of. What you're thinking of can be done like this
<?php
class stdClass1 {
private static $methods = [];
public function __call($method, $arguments) {
return call_user_func_array(Closure::bind($this->methods[$method], $this, get_called_class()), $arguments);
}
public function __set($name, $value) {
if (is_callable($value)) {
$this->methods[$name] = $value;
} else {
parent::__set($name, $value);
}
}
}
Here we're using a static, defined property to hold all of the dynamic methods, and we're using the magic __set property to set the methods in to the array.
That being said, dynamically loading methods in to an object is bad. Don't do that

Using $this in "anonymous" object

I am using the following class to mimic anonymous objects in PHP:
class AnonymousObject
{
protected $methods = array();
public function __construct(array $options) {
$this->methods = $options;
}
public function __call($name, $arguments) {
$callable = null;
if (array_key_exists($name, $this->methods))
$callable = $this->methods[$name];
elseif(isset($this->$name))
$callable = $this->$name;
if (!is_callable($callable))
throw new BadMethodCallException("Method {$name} does not exist");
return call_user_func_array($callable, $arguments);
}
}
(https://gist.github.com/Mihailoff/3700483)
Now, as long as the declared functions stand on their own everything works fine, but whenever I try to call one function from the other like this ...
$anonymous = new AnonymousObject(array(
"foo" => function() { $this->bar(); },
"bar" => function() { }
));
then of course I get Fatal error: Using $this when not in object context
Is there any way to work around this problem?
You can use the bind() or bindTo() method of the Closure instance that represents the anonymous function.
<?php
class AnonymousObject
{
protected $methods = array();
public function __construct(array $options) {
$this->methods = $options;
}
public function __call($name, $arguments) {
$callable = null;
if (array_key_exists($name, $this->methods))
$callable = $this->methods[$name];
elseif(isset($this->$name))
$callable = $this->$name;
if (!is_callable($callable))
throw new BadMethodCallException("Method {$name} does not exists");
$callable = $callable->bindTo($this);
return call_user_func_array($callable, $arguments);
}
}
$anonymous = new AnonymousObject(array(
"foo" => function() { echo 'foo'; $this->bar(); },
"bar" => function() { echo 'bar'; }
));
$anonymous->foo();
(example not quite right, since it will work only with anonymous functions; not with all the other callable() alternatives like e.g. the $this->name part)
prints foobar.

PHP implements ArrayAccess

I have two Classes viz foo & Bar
class bar extends foo
{
public $element = null;
public function __construct()
{
}
}
and the Class foo goes as
class foo implements ArrayAccess
{
private $data = [];
private $elementId = null;
public function __call($functionName, $arguments)
{
if ($this->elementId !== null) {
echo "Function $functionName called with arguments " . print_r($arguments, true);
}
return true;
}
public function __construct($id = null)
{
$this->elementId = $id;
}
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->data[] = $value;
} else {
$this->data[$offset] = $value;
}
}
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}
public function offsetUnset($offset)
{
if ($this->offsetExists($offset)) {
unset($this->data[$offset]);
}
}
public function offsetGet($offset)
{
if (!$this->offsetExists($offset)) {
$this->$offset = new foo($offset);
}
}
}
i want that when i run the below piece of code:
$a = new bar();
$a['saysomething']->sayHello('Hello Said!');
should return Function sayHello Called with arguments Hello Said! from foo's __call magic method.
Here, i want to say is saysomething should be passed in $this->elementId from foo's __construct function and sayHello should be taken as method and Hello Said should be taken as parameters for sayHello Function which would be rendered from __call magic method.
Also, need to chain methods like:
$a['saysomething']->sayHello('Hello Said!')->sayBye('Good Bye!');
If I'm not mistaken, you should change foo::offsetGet() to this:
public function offsetGet($offset)
{
if (!$this->offsetExists($offset)) {
return new self($this->elementId);
} else {
return $this->data[$offset];
}
}
It returns an instance of itself if there's no element at the given offset.
That said, foo::__construct() should be called from bar::__construct() as well and be passed a value other than null:
class bar extends foo
{
public $element = null;
public function __construct()
{
parent::__construct(42);
}
}
Update
To chain calls, you need to return the instance from __call():
public function __call($functionName, $arguments)
{
if ($this->elementId !== null) {
echo "Function $functionName called with arguments " . print_r($arguments, true);
}
return $this;
}

Questions abt PHP Magic Methods & Delegating

i was wanting to wrap an object in another - favoring composition over inheritance. but i am not sure i am doing it right tho there are no errors.
i created a class Wrapped thats is wrapped by Wrapper. i made it such that when a method/property is called on $wrapper, if it exists in the class, Wrapper, it will be returned else, it will delegate to the $wrapped object. i wonder apart from the fact i didnt check if the method/property exists, what have i done wrong? can some1 explain __callStatic() too?
class Wrapped {
protected $wrappedProp1 = 'Wrapped: Property 1';
protected $wrappedProp2 = 'Wrapped: Property 2';
function method1($arg1, $arg2) {
echo "Called Wrapped::method1() with the following parameters: $arg1, $arg2";
}
static function sMethod2() {
echo 'Called a static method in wrapped';
}
function __get($name) {
return $this->$name;
}
function __set($name, $val) {
$this->$name = $val;
}
}
class Wrapper {
protected $wrapperProp1 = 'Wrapper: Property 1';
protected $wrapped;
function __construct($wrapped) {
$this->wrapped = $wrapped;
}
function wrapperMethod() {
echo 'In wrapper method';
}
function __get($name) {
if (property_exists($this, $name)) {
return $this->$name;
}
return $this->wrapped->$name;
}
function __set($name, $val) {
if (property_exists($this, $name)) {
$this->$name = $val;
}
$this->wrapped->$name = $val;
}
function __call($name, $args = array()) {
call_user_func_array(array($this->wrapped, $name), $args);
}
static function __callStatic($name, $args = array()) {
call_user_func_array(array('Wrapped', $name), $args);
}
}
$wrapper = new Wrapper(new Wrapped);
// testing normal methods
$wrapper->wrapperMethod();
echo $wrapper->wrapperProp1;
$wrapper->wrapperProp1 = 'New Wrapper Prop 1';
echo $wrapper->wrapperProp1;
// testing delegates
$wrapper->method1('hello', 'world'); //delegated to Wrapped::method1()
$wrapper->sMethod2(); // delegated to static Wrapped::sMethod2() ... what is callStatic for then
echo $wrapper->wrappedProp2;
Wrapper::sMethod2();
As it seems - it's all ok.
About __callStatic() - it allows you to workaround undefined static functions in class.
Example:
<?php
class Foo {
static function __callStatic($name, $args = array()) {
echo "Called static function $name with arguments ". print_r($args, true);
}
}
Foo::Bar('test');
// will output "Called static function Bar with arguments Array ( 0 => test );"

Categories