It's always bugged me a recursive function needs to name itself, when a instantiated class can use $this and a static method can use self etc.
Is there a similar way to do this in a recursive function without naming it again (just to cut down on maintenance)?
Obviously I could use call_user_func or the __FUNCTION__ constant but I would prefer something less ugly.
You can make use of variable functions and declare a variable with the function name at the beginning of you function (or wherever). No need for call_user_func:
function test($i) {
$__name = __FUNCTION__;
if($i > 5) {
echo $i. "\n";
$__name($i-1);
}
}
Don't forget that using the real function name is probably more readable for other people :)
(at least provide a comment why you do this)
Update:
As #Alix mentions in his comment, it might be useful to declare $__name as static. This way, the value is not assigned over and over again to the variable.
I don't know why this is ugly:
return call_user_func_array(__FUNCTION__, func_get_args());
Versus:
return call_user_func_array('someFunction', func_get_args());
You would still need to use call_user_func_array() if you're looking to cut down on maintenance (if your functions have [a lot / a different number] of arguments).
Other than that I don't see another way. Also a static method cannot reference itself using self::, only to its class. You would also need to use the magic __METHOD__ constant to do that.
function anyfunc() {
__FUNCTION__();
}
if used in class:
protected function anymethod() {
$this->{__FUNCTION__}();
}
For those of you who want to do this within a static method:
forward_static_call(array('self', __METHOD__), $arg1, $arg2, $etc);
This way if the method is renamed you dont have to worry about changing all the recursion calls within it too.
function self(){
return call_user_func_array(debug_backtrace()[1]['function'], func_get_args());
}
function test($i) {
if($i) {
echo "$i<br>\n";
self($i-1);
}
}
test(5);
You can simply include the arguments by combining func_get_args() and the Variadic or ... added in 5.6.
As a procedural function
function foo($arg,$arg1) {
__FUNCTION__(...func_get_args());
}
As a class method:
protected function foo($arg,$arg1,$arg3, $etc) {
$this->{__FUNCTION__}(...func_get_args());
}
https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list
Coincidently this works anytime you want to inject an array of arguments into a method or function...
For example (these are also equivalent)
public function foo($arg,$arg1)
call_user_func_array([$this, __FUNCTION__], func_get_args());
}
call_user_func_array should be Deprecated in my opinion, because there is no need for it in several ways. Most obvious to me is this.
public function foo($arg,$arg1)
call_user_func([$this, __FUNCTION__], ...func_get_args());
}
The ... is quite useful (I think it plays well with named arguments, though I haven't tried that yet).
Related
In JavaScript nested functions are very useful: closures, private methods and what have you..
What are nested PHP functions for? Does anyone use them and what for?
Here's a small investigation I did
<?php
function outer( $msg ) {
function inner( $msg ) {
echo 'inner: '.$msg.' ';
}
echo 'outer: '.$msg.' ';
inner( $msg );
}
inner( 'test1' ); // Fatal error: Call to undefined function inner()
outer( 'test2' ); // outer: test2 inner: test2
inner( 'test3' ); // inner: test3
outer( 'test4' ); // Fatal error: Cannot redeclare inner()
If you are using PHP 5.3 you can get more JavaScript-like behaviour with an anonymous function:
<?php
function outer() {
$inner=function() {
echo "test\n";
};
$inner();
}
outer();
outer();
inner(); //PHP Fatal error: Call to undefined function inner()
$inner(); //PHP Fatal error: Function name must be a string
?>
Output:
test
test
There is none basically. I've always treated this as a side effect of the parser.
Eran Galperin is mistaken in thinking that these functions are somehow private. They are simply undeclared until outer() is run. They are also not privately scoped; they do pollute the global scope, albeit delayed. And as a callback, the outer callback could still only be called once. I still don't see how it's helpful to apply it on an array, which very likely calls the alias more than once.
The only 'real world' example I could dig up is this, which can only run once, and could be rewritten cleaner, IMO.
The only use I can think of, is for modules to call a [name]_include method, which sets several nested methods in the global space, combined with
if (!function_exists ('somefunc')) {
function somefunc() { }
}
checks.
PHP's OOP would obviously be a better choice :)
[Rewritten according to the comment by #PierredeLESPINAY.]
It's not just a side-effect at all, but actually a very useful feature for dynamically modifying the logic of your program. It's from the procedural PHP days, but can come in handy with OO architectures too, if you want to provide alternative implementations for certain standalone functions in the most straightforward way possible. (While OO is the better choice most of the time, it's an option, not a mandate, and some simple tasks don't need the extra cruft.)
For example, if you dynamically/conditionally load plugins from your framework, and want to make the life of the plugin authors super easy, you can provide default implementations for some critical functions the plugin didn't override:
<?php // Some framework module
function provide_defaults()
{
// Make sure a critical function exists:
if (!function_exists("tedious_plugin_callback"))
{
function tedious_plugin_callback()
{
// Complex code no plugin author ever bothers to customize... ;)
}
}
}
Functions defined within functions I can't see much use for but conditionally defined functions I can. For example:
if ($language == 'en') {
function cmp($a, $b) { /* sort by English word order */ }
} else if ($language == 'de') {
function cmp($a, $b) { /* sort by German word order; yes it's different */ }
} // etc
And then all your code needs to do is use the 'cmp' function in things like usort() calls so you don't litter language checks all over your code. Now I haven't done this but I can see arguments for doing it.
All the above being said, one might simply create a nested function to replace some localized, repetitive code within a function (that will only be used inside the parent function). An anonymous function is a perfect example of this.
Some might say just create private methods (or smaller code blocks) in a class, but that is muddying the waters when an ultra-specific task (which is exclusive to the parent) needs to be modularized, but not necessarily available to the rest of a class. The good news is if it turns out that you do need that function somewhere else, the fix is rather elementary (move the definition to a more central location).
Generally speaking, using JavaScript as the standard by which to evaluate other C based programming languages is a bad idea. JavaScript is definitely its own animal when compared to PHP, Python, Perl, C, C++, and Java. Of course, there are lots of general similarities, but the nitty, gritty details (reference JavaScript: The Definitive Guide, 6th Edition, Chapters 1-12), when paid attention to, make core JavaScript unique, beautiful, different, simple, and complex all at the same time. That's my two cents.
Just to be clear, I'm not saying nested functions are private. Just that nesting can help avoid clutter when something trivial needs to be modularized (and is only needed by the parent function).
All of my php is OO, but I do see a use for nested functions, particularly when your function is recursive and not necessarily an object. That is to say, it does not get called outside of the function it is nested in, but is recursive and subsequently needs to be a function.
There's little point in making a new method for the express use of a single other method. To me that's clumsy code and sort-of not the point of OO. If you're never going to call that function anywhere else, nest it.
In webservice calling we found it a much lower overhead (memory and speed) dynamically including in a nested fashion, individual functions over libraries full of 1000s of functions. The typical call stack might be between 5-10 calls deep only requiring linking a dozen 1-2kb files dynamically was better than including megabytes. This was done just by creating a small util function wrapping requires. The included functions become nested within the functions above the call stack. Consider it in contrast to classes full of 100s of functions that weren't required upon every webservice call but could also have used the inbuilt lazy loading features of php.
if you are in php 7 then see this:
This implementation will give you a clear idea about nested function.
Suppose we have three functions(too(), boo() and zoo()) nested in function foo().
boo() and zoo() have same named nested function xoo(). Now in this code I have commented out the rules of nested functions clearly.
function foo(){
echo 'foo() is called'.'<br>';
function too(){
echo 'foo()->too() is called'.'<br>';
}
function boo(){
echo 'foo()->boo() is called'.'<br>';
function xoo(){
echo 'foo()->boo()->xoo() is called'.'<br>';
}
function moo(){
echo 'foo()->boo()->moo() is called'.'<br>';
}
}
function zoo(){
echo 'foo()->zoo() is called'.'<br>';
function xoo(){ //same name as used in boo()->xoo();
echo 'zoo()->xoo() is called'.'<br>';
}
#we can use same name for nested function more than once
#but we can not call more than one of the parent function
}
}
/****************************************************************
* TO CALL A INNER FUNCTION YOU MUST CALL OUTER FUNCTIONS FIRST *
****************************************************************/
#xoo();//error: as we have to declare foo() first as xoo() is nested in foo()
function test1(){
echo '<b>test1:</b><br>';
foo(); //call foo()
too();
boo();
too(); // we can can a function twice
moo(); // moo() can be called as we have already called boo() and foo()
xoo(); // xoo() can be called as we have already called boo() and foo()
#zoo(); re-declaration error
//we cannont call zoo() because we have already called boo() and both of them have same named nested function xoo()
}
function test2(){
echo '<b>test2:</b><br>';
foo(); //call foo()
too();
#moo();
//we can not call moo() as the parent function boo() is not yet called
zoo();
xoo();
#boo(); re-declaration error
//we cannont call boo() because we have already called zoo() and both of them have same named nested function xoo()
}
Now if we call test1() the output will be this:
test1:
foo() is called
foo()->too() is called
foo()->boo() is called
foo()->too() is called
foo()->boo()->moo() is called
foo()->boo()->xoo() is called
if we call test2() the output will be this:
test2:
foo() is called
foo()->too() is called
foo()->zoo() is called
zoo()->xoo() is called
But we cannot call both text1() and test2() at same time to avoid re-declaration error
For those that suggest that there is no practical use of nested functions. Yes they have use and this is an example.
Imagine that I have a file called my_file.php which is used to get an ajax result out of. But what if there are times that you don't want to get the result through ajax but you want to include it twice in the same page without conflicts?
Lets say ajax file my_file.php :
<?php
// my_file.php used for ajax
$ajax_json_in = 10;
function calculations($a, $b)
{ $result = $a + $b;
return $result;
}
$new_result = $ajax_json_in * calculations(1, 2);
$ajax_json_out = $new_result;
?>
Below example includes the above file twice without conflict. You may not want to ajax call it, because there are cases that you need to include it straight in the HTML.
<?php
// include the above file my_file.php instead of ajaxing it
function result1
{
$ajax_json_in = 20;
include("my_file.php");
return $ajax_json_out;
}
function result2
{
$ajax_json_in = 20;
include("my_file.php");
return $ajax_json_out;
}
?>
Including the file makes the file's functions nested. The file is used both for ajax calls and inline includes !!!
So there is use in real life of nested functions.
Have a nice day.
I know this is an old post but fwiw I use nested functions to give a neat and tidy approach to a recursive call when I only need the functionality locally - e.g. for building hierarchical objects etc (obviously you need to be careful the parent function is only called once):
function main() {
// Some code
function addChildren ($parentVar) {
// Do something
if ($needsGrandChildren) addChildren ($childVar);
}
addChildren ($mainVar); // This call must be below nested func
// Some more code
}
A point of note in php compared with JS for instance is that the call to the nested function needs to be made after, i.e. below, the function declaration (compared with JS where the function call can be anywhere within the parent function
I have only really used this characteristic when it was useful to execute a small recursive function inside a primary, more categorical function, but didn't want to move it to a different file because it was fundamental to the behavior of a primary process. I realize there are other "best practice" ways of doing this, but I want to make sure my devs see that function every time they look at my parser, it's likely what they should modify anyway...
Nested functions are useful in Memoization (caching function results to improve performance).
<?php
function foo($arg1, $arg2) {
$cacheKey = "foo($arg1, $arg2)";
if (! getCachedValue($cacheKey)) {
function _foo($arg1, $arg2) {
// whatever
return $result;
}
$result = _foo($arg1, $arg2);
setCachedValue($cacheKey, $result);
}
return getCachedValue($cacheKey);
}
?>
Nested functions are useful if you want the nested function to utilize a variable that was declared within the parent function.
<?php
ParentFunc();
function ParentFunc()
{
$var = 5;
function NestedFunc()
{
global $var;
$var = $var + 5;
return $var;
};
echo NestedFunc()."<br>";
echo NestedFunc()."<br>";
echo NestedFunc()."<br>";
}
?>
Sometimes, especially with callbacks functions or inheritance/implementation case, I don't want to use some arguments in method. But they are required by the method interface signature (and I can't change the signature, let's say it's something required via Composer). Example:
// Assuming the class implements an interface with this method:
// public function doSomething($usefull1, $usefull2, $usefull3);
public function doSomething($usefull, $useless_here, $useless_here) {
return something_with($usefull);
}
// ...
In some other languages (let's say Rust), I can ignore these arguments explicitly, which make the code (and intention) more readable. In PHP, it could be this:
public function doSomething($usefull, $_, $_) {
return something_with($usefull);
}
Is this possible in PHP? Did I missed something?
Side note: it's not only for trailing arguments, it could be anywhere in the function declaration
I think the best you can hope for is to give them unique names that will suggest that they will not be used in the call.
Perhaps:
function doSomething($usefull,$x1,$x2){
return something_with($usefull);
}
Or:
function doSomething($ignore1,$useful,$ignore2){
return something_with($useful);
}
PHP wants to have arguments accounted for and uniquely named.
Edit: If you want to avoid declaring variables that you won't use (but you know they are being sent), try func_get_args() and list(). This should make the code lean, clean, readable. (Demo)
function test(){
// get only use argument 2
list(,$useful,)=func_get_args();
echo $useful;
}
test('one','two','three'); // outputs: two
Assign default value for optional parameter.
function doSomething($usefull,$useless1=null,$useless2=null){
return something_with($usefull);
}
Now....
Parameter 1 is required
Parameter 2 is optional
Parameter 3 is optional
Call function like..
doSomething($data);
doSomething($data,$anotherData);
doSomething($data,$anotherData,$anotherData1);
Your concrete object does not fit the interface completly, so you can just add a adapter class between them. So the interface stay as it is and your object just get what it really need.
class Adapter extends CustomInterface
{
function doSomething($ignore1,$useful,$ignore2){
return $customClass->something_with($useful);
}
}
I have two classes that I use to access two different tables in my db. They both have a similar constructor that looks like that:
function __construct($db) {
$this->db = $db;
$userDAO = DAO_DBrecord::createUserDAO($this->db);
$this->userDAO = $userDAO;
}
The other class has the same constructor except that it uses createOtherTableDAO($this->db).
I am planning on having a couple other such classes, and it would be convenient if I could have them all inherit the same constructor, and pass createAppropriateTableDAO as an argument.
To clarify, in the first case above, createUserDAO($this->db) is a static function that calls a constructor in my DAO class. The function in the DAO looks as follows:
public static function createUserDAO($db) {
return new DAO_DBrecord($db, 'users');
}
I use this method to make sure the user model can only call a DAO on the users table.
I'm somewhat of a beginner, and I don't think I have ever seen anything like what I want.
Move the code to create the DAOs into a Factory and then inject the DAOs instead of hard coupling them into whatever these classes are supposed to represent. Or rather create the various Table Data Gateways ("classes that I use to access two different tables") as a whole in the Factory, e.g.
class TableDataGatewayFactory
…
public function create($gatewayName)
{
switch ($gatewayName) {
case 'user':
return new TableDataGateway(new UserDao($this->db)));
break;
default:
throw new Exception('No Gateway for $gatewayName');
}
}
}
As for $this->db, either pass that into the Factory via the ctor or move the creation into the Factory as well. It's somewhat doubled responsibility, but tolerable given that this Factory revolved around creating Database related collaborator graphs.
Apart from that: yes, call_user_func(array('ClassName', 'methodName')) would work. See the manual for
http://php.net/call_user_func and
http://php.net/manual/en/language.pseudo-types.php#language.types.callback
To answer your question first: No, you can't (without resorting to evilCode) pass a function name as a parameter.
But: What you want to archive is a poster-child-issue for an object oriented approach using inheritance.
You'd need a base-class:
class BaseClass
{
function __construct($db) {
$this->db = db;
}
}
and your implementations :
class MyClass extends BaseClass
{
function __construct($db) {
parent::__contruct($db);
$this->userDAO = DAO_DBrecord::createUserDAO($this->db);
}
}
Just for the record: the evilCode would have been
a) you could encapsulate your function in a create_function that can be used as an argument.
b) you could pass the function name as a string to your function and then pass it to eval in the receiving function.
But remember: When eval or create_function looks like the answer you're probably asking the wrong questions!
See: related question
There are several methods which you can use if you feel it necessary to pass the function name or indeed the function itself as a parameter of a function.
call_user_func($function,$args);
call_user_func is one of Php's native functions for invoking methods or functions which takes a function name and optional arguments parameter.
The functionality of call_user_func (when not pertaining to object methods) can be replicated without the using call_user_func using a variable with the string literal of the function name. For example:
function some_func()
{
echo "I'm a function!";
}
$function = "some_func";
$function(); /*Output: I'm a function!*/
And if you're feeling adventurous you can go a bit further and pass a closure / anonymous function as instead of the function name. For example:
$function = function()
{
echo "I'm another function!";
}
$function(); /*Output: I'm another function*/
You can achieve such behavior by using:
call_user_func
eval any literal
I would like to define a class constant using a concatenation of an existing constant and a string. I can't predefine it because only scalars are allowed for predefining constants, so I currently have it as part of my constructor with a defined() function checking if it is already defined. This solution works but my constant is now unnecessarily global.
Is there a way to define a class constant at runtime in php?
Thank you.
See the PHP manual on Class constants
The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.
In other words, it is not possible. You could do it with runkit_constant_add but this sort of monkey patching is strongly discouraged.
Another option is to use the magic methods __get() and __set() to reject changes to certain variables. This is not so much a constant as a read-only variable (from the perspective of other classes). Something like this:
// Completely untested, just an idea
// inspired in part from the Zend_Config class in Zend Framework
class Foobar {
private $myconstant;
public function __construct($val) {
$this->myconstant = $val;
}
public function __get($name) {
// this will expose any private variables
// you may want to only allow certain ones to be exposed
return $this->$name;
}
public function __set($name) {
throw new Excpetion("Can't set read-only property");
}
}
You cannot do exactly what you want to do, per Gordon's answer. However, you can do something like this. You can only set it once:
class MyClass
{
private static $myFakeConst;
public getMyFakeConst()
{
return self::$myFakeConst;
}
public setMyFakeConst($val)
{
if (!is_null(self::$myFakeConst))
throw new Exception('Cannot change the value of myFakeConst.');
self::$myFakeConst = $val;
}
}
I know that it is possible to create a function within another function. Why might one need to do that in real life? (PHP)
function someFunction($var)
{
function anotherFunction()
{
return M_PI;
}
return anotherFunction();
}
The only time you'd ever really want to define a function inside of another function is if you didn't want that inner function available to anything until the outer function is called.
function load_my_library() {
...
function unload_my_library() {
}
}
The only time you'd need (or want) unload_my_library to be available is after the library has been loaded.
Nested functions generally shouldn't ever be used. Classes and public/private methods solve the same sorts of problems much more cleanly.
However, function generating functions can be useful:
<?php
# requires php 5.3+
function make_adder($a)
{
return function($b) use($a) {
return $a + $b;
};
}
$plus_one = make_adder(1);
$plus_fortytwo = make_adder(42);
echo $plus_one(3)."\n"; // 4
echo $plus_fortytwo(10)."\n"; // 52
?>
This example is contrived and silly, but this sort of thing can be useful for generating functions used by sorting routines, etc.
I'm guessing here, but I believe that it is used for passing the function to another function for execution. For example, calling a search function where you can specify a callback function to perform the search order comparison. This will allow you to encapsulate the comparator in your outer function.