Bug #68026 [Com]: create_function() fails
| From: | jmichae3 at yahoo dot com | Date: | Wed, 17 Sep 2014 09:10:15 +0000 |
| Subject: | Bug #68026 [Com]: create_function() fails | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-187564@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68026&edit=1
ID: 68026
Comment by: jmichae3 at yahoo dot com
Reported by: jmichae3 at yahoo dot com
Summary: create_function() fails
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Windows 7 pro x64 sp1
PHP Version: 5.5.16
Block user comment: N
Private report: N
New Comment:
Your code to test function creation should be
!is_callable(${'is'.$AcctTypes[$i].'file'})
But please, do a favor for everyone who has to deal with your code:
1. Stop using globals
2. Don't use variable variables
3. Don't dynamically create important functions
is_callable():
your syntax is wrong according to the manual, and anyway it fails when I use it:
"Stop using globals":
no law against using globals. unless the global keyword is only for making an outside variable
visible inside a function, in which case you need to say that in the manual, so that's a
documentation bug, because that's not stated. I wanted to declare my vars with global-scope and
have it visible all throughout my code. they are really important variables.
"Don't dynamically create important functions":
in other words, don't rely on the spec or any functions in the manual, throw the manual away.
if a given function is documented as being there, php.net is not really giving it to you, it's
just virtual? do you realize what you just said and how backwards that sounds?
ERROR: lamba function isadminfile() doesn't exist.
ERROR: lamba function name isadminfile is not callable.
ERROR: lamba function ispaidblsfile() doesn't exist.
ERROR: lamba function name ispaidblsfile is not callable.
ERROR: lamba function ispaidmusicfile() doesn't exist.
ERROR: lamba function name ispaidmusicfile is not callable.
ERROR: lamba function isuserfile() doesn't exist.
ERROR: lamba function name isuserfile is not callable.
so you are basically saying stop writing code period? I am just following spec. everything I am
trying are things the manual says I should be able to do, like variable variables etc. I fully
expect them to work. if it doesn't work, then fix the bugs please. found 2 PHP bugs already
just trying to convert my code to a new way of doing things.
Previous Comments:
------------------------------------------------------------------------
[2014-09-17 08:25:44] requinix@php.net
This is not the place for an in-depth explanation of why your code is wrong. Please re-read what I
said, or try an online forum or mailing list.
http://php.net/support.php
------------------------------------------------------------------------
[2014-09-17 08:09:50] jmichae3 at yahoo dot com
you dismissed the bug out of hand, it's still valid.
for ($i=0; $i < count($AcctTypes); $i++) {
//global ${'is'.$AcctTypes[$i].'file'};
//in our web site, we detect if the /admin/ directory
$funcbody =' return ((strpos($filepath,"/'.$AcctTypes[$i].'/") !==
false';
$funcbody.= ' && strpos($filepath,"/'.$AcctTypes[$i].'/")
<=3));'; //is this in the /admin/ or /moderator/ etc directory off root?
//create the function from the function body in $funcbody with name in $funcname
${'is'.$AcctTypes[$i].'file'}=create_function('$filepath',
$funcbody);
//aggregate any failures so we can see what's wrong and fix it
$anyError=false;
if (!isset(${'is'.$AcctTypes[$i].'file'})) {
echo "ERROR: lambda function name variable
".'is'.$AcctTypes[$i].'file'." failed isset().\n";
$anyError=true;
}
if (!function_exists('is'.$AcctTypes[$i].'file')) {
echo "ERROR: lamba function
".'is'.$AcctTypes[$i].'file'."() doesn't exist.\n";
$anyError=true;
}
}
if ($anyError) {
die("----please fix code.\n");
}
output:
ERROR: lamba function isadminfile() doesn't exist.
ERROR: lamba function ispaidblsfile() doesn't exist.
ERROR: lamba function ispaidmusicfile() doesn't exist.
ERROR: lamba function isuserfile() doesn't exist.
----please fix code.
regardless of whether it contained a functoin name or not, its contents would have been replaced
with a function (supposedly) by the call to create_function, would it not?
what I needed was to make a variable with a variable name (which I could base on a for loop to make
a number of functions) that is also a function name. but this is consistently failing.
------------------------------------------------------------------------
[2014-09-16 17:27:18] requinix@php.net
>The $isadminfile variable contains a function.
Well, technically it contains a function name, not a closure.
------------------------------------------------------------------------
[2014-09-16 17:26:00] requinix@php.net
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
You have not defined a function named "isadminfile". The $isadminfile variable contains a
function. So in code where you want to use them you'd have to do
global $isadminfile;
$return = $isadminfile($filepath);
Your code to test function creation should be
!is_callable(${'is'.$AcctTypes[$i].'file'})
But please, do a favor for everyone who has to deal with your code:
1. Stop using globals
2. Don't use variable variables
3. Don't dynamically create important functions
------------------------------------------------------------------------
[2014-09-16 09:26:14] jmichae3 at yahoo dot com
Description:
------------
create_function doesn't seem to actually create the function.
Test script:
---------------
$AcctTypes=preg_split('/,/', 'admin,paidbls,paidmusic,user');
for ($i=0; $i < count($AcctTypes); $i++) {
global ${'is'.$AcctTypes[$i].'file'};
$funcbody='{ global $AcctTypes;';
$funcbody.=" if (strpos(\$filepath,\"/".$AcctTypes[$i]."/\") !==
FALSE";
$funcbody.=" && strpos(\$filepath,\"/".$AcctTypes[$i]."/\")
!== ''";
$funcbody.=" && strpos(\$filepath,\"/".$AcctTypes[$i]."/\")
<=3) {";
$funcbody.=' return TRUE;';
$funcbody.=' } else {';
$funcbody.=' return FALSE;';
$funcbody.=' } }';
//create the function from the function body in $funcbody with name in $funcname
${'is'.$AcctTypes[$i].'file'}=create_function('$filepath',
$funcbody);
}
for ($i=0; $i < count($AcctTypes); $i++) {
if (!function_exists('is'.$AcctTypes[$i].'file')) {
die("\n\nERROR:config file needs function
".'is'.$AcctTypes[$i].'file'."()\n\n");
}
}
Expected result:
----------------
functions created, no output.
Actual result:
--------------
( my error when function not defined)
ERROR:config file needs function isadminfile()
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68026&edit=1