Anonymous function for a method of an object [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Calling closure assigned to object property directly
Why this is not possible in PHP? I want to be able to create a function on the fly for a particular object.
$a = 'a';
$tokenMapper->tokenJoinHistories = function($a) {
echo $a;
};
$tokenMapper->tokenJoinHistories($a);

PHP tries to match an instance method called "tokenJoinHistories" that is not defined in the original class
You have to do instead
$anon_func = $tokenMapper->tokenJoinHistories;
$anon_func($a);
Read the documentation here especially the comment part.

With $obj->foo() you call methods, but you want to call a property as a function/method. This just confuses the parser, because he didn't find a method with the name foo(), but he cannot expect any property to be something callable.
call_user_func($tokenMapper->tokenJoinHistories, $a);
Or you extend your mapper like
class Bar {
public function __call ($name, $args) {
if (isset($this->$name) && is_callable($this->$name)) {
return call_user_func_array($this->$name, $args);
} else {
throw new Exception("Undefined method '$name'");
}
}
}
(There are probably some issues within this quickly written example)

Related

Get time difference between db time values and today's time in mysql [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
So I'm refactoring my code to implement more OOP. I set up a class to hold page attributes.
class PageAtrributes
{
private $db_connection;
private $page_title;
public function __construct($db_connection)
{
$this->db_connection = $db_connection;
$this->page_title = '';
}
public function get_page_title()
{
return $this->page_title;
}
public function set_page_title($page_title)
{
$this->page_title = $page_title;
}
}
Later on I call the set_page_title() function like so
function page_properties($objPortal) {
$objPage->set_page_title($myrow['title']);
}
When I do I receive the error message:
Call to a member function set_page_title() on a non-object
So what am I missing?
It means that $objPage is not an instance of an object. Can we see the code you used to initialize the variable?
As you expect a specific object type, you can also make use of PHPs type-hinting featureDocs to get the error when your logic is violated:
function page_properties(PageAtrributes $objPortal) {
...
$objPage->set_page_title($myrow['title']);
}
This function will only accept PageAtrributes for the first parameter.
There's an easy way to produce this error:
$joe = null;
$joe->anything();
Will render the error:
Fatal error: Call to a member function anything() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/casMail/dao/server.php on line 23
It would be a lot better if PHP would just say,
Fatal error: Call from Joe is not defined because (a) joe is null or (b) joe does not define anything() in on line <##>.
Usually you have build your class so that $joe is not defined in the constructor or
Either $objPage is not an instance variable OR your are overwriting $objPage with something that is not an instance of class PageAttributes.
It could also mean that when you initialized your object, you may have re-used the object name in another part of your code. Therefore changing it's aspect from an object to a standard variable.
IE
$game = new game;
$game->doGameStuff($gameReturn);
foreach($gameArray as $game)
{
$game['STUFF']; // No longer an object and is now a standard variable pointer for $game.
}
$game->doGameStuff($gameReturn); // Wont work because $game is declared as a standard variable. You need to be careful when using common variable names and were they are declared in your code.
function page_properties($objPortal) {
$objPage->set_page_title($myrow['title']);
}
looks like different names of variables $objPortal vs $objPage
I recommend the accepted answer above. If you are in a pinch, however, you could declare the object as a global within the page_properties function.
$objPage = new PageAtrributes;
function page_properties() {
global $objPage;
$objPage->set_page_title($myrow['title']);
}
I realized that I wasn't passing $objPage into page_properties(). It works fine now.
you can use 'use' in function like bellow example
function page_properties($objPortal) use($objPage){
$objPage->set_page_title($myrow['title']);
}

Pass an array of interfaces into a function? [duplicate]

This question already has answers here:
Type hinting - specify an array of objects
(6 answers)
Closed 6 years ago.
In PHP since the interface benefits can used by passing as parameter mentioning the Interface name something like
public function foo (Abc $abc){}
where Abc is an interface.But how do I pass an array of these interfaces?
Please note this not class but interface and only way to get advantage of interface is passing as function with type hinting
In PHP 5.6+ you could do something like this:
function foo(Abc ...$args) {
}
foo(...$arr);
foo() takes a variable amount of arguments of type Abc, and by calling foo(...$arr) you unpack $arr into a list of arguments. If $arr contains anything other than instances of Abc an error will be thrown.
This is a little 'hacky', but it's the only way to get type hinting for an array in PHP, without putting down some extra code.
Unfortunately, you cannot check for two different interfaces at the same time using type hinting in PHP, but you can write a function for this which will check if the object belongs to multiple interfaces, eg -
function belongs_to_Interfaces($obj,array $interfaces)
{
foreach($interfaces as $interface)
{
if(!is_a($obj,$interface))
{
return false;
}
}
return true;
}
You can then use it like this,
public function foo ($abc){
if(!belongs_to_Interfaces($abc, ['interface1', 'interface2'])){
//throw an error or return false
}
}
if you use PHP 5.6+ you can use variadic with decorator pattern:
<?php
interface Rule {
public function isSatisfied();
}
final class ChainRule implements Rule {
private $rules;
public function __construct(Rule ...$rules) {
$this->rules = $rules;
}
public function isSatisfied() {
foreach($this->rules as $rule)
$rule->isSatisfied();
}
}

php oop reference 'parent' object from an injected object [duplicate]

This question already has answers here:
Parent Object in php
(6 answers)
Closed 9 years ago.
Is there a way for an object to be aware of the object by which it was intstantiated?
For some reason (it really goes too far to explain why), I would like to achieve something like:
class outer{
public $x ;
function __CONSTRUCT(){
$this->x = "hello world";
$innerObject = $new inner();
}
function echo_x(){
echo $this->x;
}
}
class inner() {
function echo_var_from_outer(){
parent::echo_x();
// the above won't work
}
}
$bar = new outer();
$bar->innerObject->echo_var_from_outer();
Of course I could pass the a reference for the outer class to the inner class, but it would really help me if that weren't necessary. I do know a lot of workarounds for this problem but that is not what I'm looking for. Please tell me if an injected object has any implicit awareness of the object that instantiated it.
In your inner class do the following
class inner extends outer {
function echo_var_from_outer(){
parent::echo_x();
// the above won't work
}
}

Object Oriented Programming -> Operator [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
So I've been reading through the book PHP Solutions, Dynamic Web Design Made Easy by David Powers. I read through the short section on Object Oriented PHP, and I am having a hard time grasping the idea of the -> operator. Can anyone try to give me a solid explanation on the -> operator in OOP PHP?
Example:
$westcost = new DateTimeZone('America/Los_Angeles');
$now->setTimezone($westcoast);
Also,a more general example:
$someObject->propertyName
The -> operator in PHP refers to either a function or a variable inside a class.
<?php
class Example {
public $variableInClass = "stringContent";
public function functionInClass() {
return "functionReturn";
}
}
$example = new Example();
var_dump($example->variableInClass); //stringContent
var_dump($example->functionInClass()); //functionReturn
?>
Do note that if we're talking about static classes (different purpose), you use :: instead:
<?php
class Example {
public static $variableInClass = "stringContent";
public static function functionInClass() {
return "functionReturn";
}
}
var_dump($example::$variableInClass); //stringContent
var_dump($example::functionInClass()); //functionReturn
?>
$someObject->propertyName can be read as:
return value stored in propertyName from object $someObject
$someObject->methodName() can be read as:
execute methodName from object $someObject
Classes and objects 101:
A class is defined as such:
class MyClass {
public $value1;
public function getValue() {
return $this->value;
}
}
We now defined a class with a single property, and a single function. To use these, we need to create an 'instance' of this object:
$myObject = new MyClass();
To use the property or function, we use the -> operator:
echo $myObject->value1;
echo $myObject->getValue();
Put a little bit more abstractly.. the function getValue is defined in this object. By using the -> operator on an instance of our class, what PHP does is effectively just call the function, just like any other function.. but before it gets called $this is assigned to the current object.
Hope this helps, if not.. I would simply recommend reading about OOP basics.

call_user_func_array passing arguments to a constructor [duplicate]

This question already has an answer here:
How to call the constructor with call_user_func_array in PHP
(1 answer)
Closed 7 years ago.
I have searched many a page of Google results as well as here on stackoverflow but cannot find a solution that seems to fit my situation. I appear to have but one last snag in the function I am trying to build, which uses call_user_func_array to dynamically create objects.
The catchable fatal error I am getting is Object of class Product could not be converted to string. When the error occurs, in the log I get five of these (one for each argument): PHP Warning: Missing argument 1 for Product::__construct(), before the catchable fatal error.
This is the code of the function:
public static function SelectAll($class, $table, $sort_field, $sort_order = "ASC")
{
/* First, the function performs a MySQL query using the provided arguments. */
$query = "SELECT * FROM " .$table. " ORDER BY " .$sort_field. " " .$sort_order;
$result = mysql_query($query);
/* Next, the function dynamically gathers the appropriate number and names of properties. */
$num_fields = mysql_num_fields($result);
for($i=0; $i < ($num_fields); $i++)
{
$fetch = mysql_fetch_field($result, $i);
$properties[$i] = $fetch->name;
}
/* Finally, the function produces and returns an array of constructed objects.*/
while($row = mysql_fetch_assoc($result))
{
for($i=0; $i < ($num_fields); $i++)
{
$args[$i] = $row[$properties[$i]];
}
$array[] = call_user_func_array (new $class, $args);
}
return $array;
}
Now, if I comment out the call_user_func_array line and replace it with this:
$array[] = new $class($args[0],$args[1],$args[2],$args[3],$args[4]);
The page loads as it should, and populates the table I am building. So everything is absolutely functional until I try to actually use my $args array within call_user_func_array.
Is there some subtle detail about calling that array that I am missing? I read the PHP manual for call_user_func_array once, and then some, and examples on that page seemed to show people just building an array and calling it for the second argument. What could I be doing wrong?
You can't call the constructor of $class like this:
call_user_func_array (new $class, $args);
That's no valid callback as first parameter. Let's pick this apart:
call_user_func_array (new $class, $args);
Is the same as
$obj = new $class;
call_user_func_array ($obj, $args);
As you can see, the constructor of $class has been already called before call_user_func_array comes into action. As it has no parameters, you see this error message:
Missing argument 1 for Product::__construct()
Next to that, $obj is of type object. A valid callback must be either a string or an array (or exceptionally a very special object: Closure, but that's out of discussion here, I only name it for completeness).
As $obj is an object and not a valid callback, so you see the PHP error message:
Object of class Product could not be converted to string.
PHP tries to convert the object to string, which it does not allow.
So as you can see, you can't easily create a callback for a constructor, as the object yet not exists. Perhaps that's why you were not able to look it up in the manual easily.
Constructors need some special dealing here: If you need to pass variable arguments to a class constructor of a not-yet initialize object, you can use the ReflectionClass to do this:
$ref = new ReflectionClass($class);
$new = $ref->newInstanceArgs($args);
See ReflectionClass::newInstanceArgs
Not possible using call_user_func_array(), because (as the name suggest) it calls functions/methods, but is not intended to create objects, Use ReflectionClass
$refClass = new ReflectionClass($class);
$object = $refClass->newInstanceArgs($args);
Another (more design-based) solution is a static factory method
class MyClass () {
public static function create ($args) {
return new self($args[0],$args[1],$args[2],$args[3],$args[4]);
}
}
and then just
$object = $class::create($args);
In my eyes it's cleaner, because less magic and more control
I use this for singleton factory pattern, becouse the ReflectionClass brokes the dependence tree, I hate the use of eval but its the only way to i find to simplificate the use of singleton pattern to inject mockObjects whith PHPUnit whitout open the class methods to that injection, BE CAREFULL WHITH THE DATA WHAT YOU PASS TO eval FUNCTION!!!!!!!! YOU MUST BE SURE THAT IS CLEANED AND FILTERED!!!
abstract class Singleton{
private static $instance=array();//collection of singleton objects instances
protected function __construct(){}//to allow call to extended constructor only from dependence tree
private function __clone(){}//to disallow duplicate
private function __wakeup(){}//comment this if you want to mock the object whith php unit jejeje
//AND HERE WE GO!!!
public static function getInstance(){
$a=get_called_class();
if(!array_key_exists($a, self::$instance)){
if(func_num_args()){
/**HERE IS THE CODE **//
$args=func_get_args();
$str='self::$instance[$a]=new $a(';
for($i=0;$i<count($args);$i++){
$str.=(($i)?",":"").'$args['.$i.']';
}
eval($str.");");//DANGER, BE CAREFULLY...we only use this code to inject MockObjects in testing...to another use you will use a normal method to configure the SingletonObject
/*--------------------------*/
}else{
self::$instance[$a]=new $a();
}
}
return self::$instance[$a];
}
}
And to use that:
class MyClass extends Singleton{
protected function __construct(MyDependInjection $injection){
//here i use the args like a normal class but the method IS PROTECTED!!!
}
}
to instanciate the object:
$myVar= MyClass::getInstance($objetFromClassMyDependInjection);
it calls the constructor whith the args I pased. i know that i can get the same result extending the static method getInstance but to teamworking its more easy to use this way

Categories