Let's assume we have this function
<?php
function run($callback)
{
$callback($some, $params);
}
How to call it?
run($someObject->callback);
or rather
run([$someObject, 'callback']);
The first one seems better to me especially because of code suggestion but in documentation is used array notation.
Why is first one worse than second one?
The array notation is better because the arrow notation doesn't work. Functions in PHP aren't first class objects which can be passed around. Whenever you "pass a function", you must pass it by name; i.e. only its name. To pass an object method, you need to pass the object and the method name using the callable pseudo type notation.
Related
var_dump(is_a([], "array"));
The above code displays "false".
But,
echo gettype([]);
Displays "array".
Am I doing something wrong or missing out on something here?
is_a() function checks if the object is of this class or has this class as one of its parents
To check if something is an array, you should use is_array().
Based on the question and what has been described in the comments, it sounds like you're looking for a way to identify if something (i.e an array or object) is iterable.
In PHP, this can be achieved with is_iterable(), which will return true if the passed-in parameter supports iteration via foreach.
From the docs:
Verify that the contents of a variable is accepted by the iterable
pseudo-type, i.e. that it is either an array or an object implementing
Traversable
In the case of the array you mentioned in the question:
is_iterable([]));
Outputs:
bool(true);
I have created two code snippets at
http://codepad.viper-7.com/o5MxgF
and http://codepad.viper-7.com/qUpTag
In the second snippet, I was trying to use array as a call back because I found at
http://www.php.net/manual/en/language.types.array.php
that
Note: Both square brackets and curly braces can be used
interchangeably for accessing array elements (e.g. $array[42] and
$array{42} will both do the same thing in the example above).
So, I thought I could use array as a function so that I don't need the declare
$get_new_key = function ($k) use($fa) {
return $fa[$k];
};
But as you can see, I was getting a
: array_map() expects parameter 1 to be a valid callback, first array
member is not a valid class name or object in /code/qUpTag on line 8
error.
Are there anything to make array a callable without creating a companion function to access its values?
Thanks.
Having a callable array doesn't mean that it returns the values of the array. Instead, it's an array that points to a method on either a class or object.
For instance, array('MyClass', 'my_method') is a callable for the static method my_method on MyClass and array($object, 'method') is a callable for the instance method method on the object in $object.
You can read more about the callable type in the PHP documentation
I have a function that requires information to be passed to it. The information is contained within an object. Therefore I must pass that object as one of the function arguments. The object is very large however, and I would like to reduce the overhead involved in making copies every time it is passed. Here is an example of
My function Call:
1 myFunction($myObject1);
and the function:
2 function myFunction($myObject2){
3 //do stuff
4 }
I understand there is more to it in php than just pass-by-reference vs pass-by-value. Correct me if I am wrong, but I believe on line 1 there is only a reference to the object made, but on line 2 the object is copied. To avoid this copy I have replaced ($myObject2) with (&$myObject2). I still refer to the object within the function definition as $myObject2 and everything seems to work. I believe I am now using a reference only and therefore making no copies of the object (which was my goal). Is my thinking correct? If not not why?
In PHP5, "objects" are not values. The value of the variables $myObject1, $myObject2 are object references (i.e. pointers to objects). You cannot get "the object itself"; objects can only be manipulated through these pointers.
Assignment and passing by value only copy values. Since objects are not values, they cannot ever be cloned through assignment, passing, etc. The only way to duplicate an object is to use the clone operator.
Putting & on a variable makes it pass or assign by reference, instead of by value without the &. Passing by reference allows you to modify the variable passed. Since the value of a variable cannot be an object, this has nothing to do with objects.
OK, I know it sounds weird but I need to make a function that will receive two parameters the first one is a string and the second an array (containing strings).
This function will then call sprintf. My first parameter will be $format and my array will correspond to the various $args.
How can I achieve this (if possible)?
Thanks!
Well you want the vsprintf() function.
Like Orbling answered, for this particular case you need vsprintf.
But, in a generic case, to call a function with variable number of parameters, you can use func_get_args() inside the function which you desire to accept multiple (any number of variable arguments). This function (when called inside your function) returns an array containing all the parameters passed while calling your function.
I'm trying to write a function that formats every (string) member/variable in an object, for example with a callback function. The variable names are unknown to me, so it must work with objects of all classes.
How can I achieve something similar to array_map or array_walk with objects?
use get_object_vars() to get an associative array of the members, and use the functions you mentioned.
btw, you can also do a foreach on an object like you would on an array, which is sometimes useful as well.
You can use get_object_vars(), but if you need more control, try using reflection. It's slower than get_object_vars() (or get_class_methods() for that matter), but it's much more powerful.
You are looking for get_object_vars / get_class_methods (the first gets the variables, the second the method names).