Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am from Java background.
In Java, every method has case-sensitive while calling. But in PHP, I didn't see the case-sensitive function name while calling the functions.
class Sample {
...
...
function sampleFunction() {
....
....
}
}
$obj = new Sample();
$obj->sampleFunction(); /* Proper call with function name */
$obj->samplefunction(); /* It should show undefined function error but it also calls sampleFunction() */
Can anyone clear my doubt why this is also called even non-case sensitive function name. And please give me how to restrict in PHP?
Thanks in advance.
They are case insensitive, see this:
Note: Function names are case-insensitive, though it is usually good
form to call functions as they appear in their declaration.
http://www.php.net/manual/en/functions.user-defined.php
Functions are not case sensitive, Variables are case sensitive.
you can read more info from manual :
http://fr.php.net/manual/en/functions.user-defined.php
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is not a duplicate of "In PHP, how do I check if a function exists?".
http://us3.php.net/function_exists will fail if input is a language
construct (e.g. http://us1.php.net/empty).
http://us1.php.net/is_callable with $syntax_only = true will allow
language constructs (e.g. empty), though it will also allow
random_function_that_does_not_exist.
How do I check if input is callable, either as an existing function or a language construct that behaves like a function?
There are all kinds of potential problems with what you're asking. However if I understand you right, you simply want to know if somename can be called like somename('someinput').
If that's true, then it appears you need to use a combination of function_exists and a manual lookup of language constructs from the List of Keywords.
Something like this perhaps:
function canICall($function) {
$callableConstructs = array("empty","unset","eval","array","exit","isset","list");
return function_exists($function) || in_array($function, $callableConstructs);
}
That $callableConstructs array is not complete, look at the List of Keywords page to build it out.
Yes it is hackish, but without a built-in way to do this in PHP I don't see another option.
Note that just because you can call something like a function, does not make it a function, nor does it mean that it behaves like a function in other ways.
You cannot call it dynamically:
$var = "empty";
$var('someinput'); // Does NOT work
Nor does this work:
call_user_func('empty', $foo);
You could use it in an eval call, but I hope you understand the huge list of reasons why that can be dangerous.
Since empty always exists and is a language construct which cannot be called using variable functions, it doubly makes no sense to want to use function_exists on it. Even if function_exists would work, it is not possible to write this code:
$func = 'empty';
if (function_exists($func)) {
$func($var);
}
The only way to call empty() is to write it literally in your source code. You cannot call it using variable functions any more than you can do that with list() = or /.
The best you could do is:
if (function_exists('empty')) {
empty($foo);
}
But because empty always exists, what's the point in testing for it?
If you want to make a validation rule, simply write your own function:
function isEmpty($value) {
return !$value;
}
$rule = 'isEmpty';
This does exactly the same thing.
Or you make empty a special case in your rule execution:
function validate($rule, $value) {
switch ($rule) {
case 'empty' :
return !$value;
default :
if (!function_exists($rule)) {
throw new InvalidArgumentException("$rule does not exist");
}
return $rule($value);
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Sorry for another noob question, but... Can someone please explain to me what the function myfunction is actually doing. I understand it's checking if the variables $a and $b are identical and than it's suppose to return 0 if they're identical but the next return is confusing. I see their using the ternary operators.
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","b"=>"green","d"=>"blue");
$a3=array("e"=>"yellow","a"=>"red","d"=>"blue");
$result=array_diff_uassoc($a1,$a2,$a3,"myfunction");
print_r($result);
the print_r returns
Array ( [c] => blue )
but how did we get here...
As stated in the documentation of array_diff_uassoc, it returns the entries from first argument that are unique compared to other arguments. And the last argument is the name of the function it uses to check whether item is unique or not.
So because only $a1 contains 'c'=>'blue' it is returned.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
why i get this error? how fix it? when run on server get Fatal error: Method name must be a string line 8
class Model extends Core_Model_Config_Data
{
protected function Load()
{
$a = file_properties();
$x0 = $this->$a["x0"](); line 8 error
$x0 = $this->$a["x1"]($x0);
$this->$a["x2"]($x0);
}
}
please help me.
Try this:
protected function Load()
{
$a = file_properties();
$f = $a['x0'];
$x0 = $this->$f();
$f = $a['x1'];
$x0 = $this->$f($x0);
$f = $a['x2'];
$this->$f($x0);
}
Obviously the values $a['x0'], $a['x1'], $a['x2'] must be strings and hold a valid method name for the class.
Right, there's a variety of possible reasons for your code throwing up errors:
What is $a["x0"]? is it a string? If so, does the method even exist?
Ambiguity: The preprocessor might have a hard time working out what you're trying to do, should the string value of $a be used to reference a property, that might be an array, that has a key "x0", which in turn might be a Closure instance, or a string that is a method name? use cruly braces to be clear: $this->{$a["x0"]}();
At no point are you cheking if the method exists, let alone if it can be called... where are your method_exists($this, $a["x0"]) and is_callable(array($this, $a["x0"])) checks?
Your code is flawed from the off, it's so incredibly error-prone... I wouldn't even bother working this one out. I'd set about rethinking my approach to whatever problem you're trying to solve here, and start over.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi Dreamweaver says there is a syntax error on the following line. Is it right?
if (!in_array(array_reverse(explode(".",strtolower($file['name'])))[0],$allowedExtensions))
Indexing the return value of a function is not supported in the version of PHP in use. Upgrade to a newer version, or turn it into separate statements.
It is probably not parsing for php 5.4 which is what is required to understand the function()[0] syntax.
Also your parameted for in_array() are incorrect
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
Dreamweaver is the CULPRIT !
Your code is just fine. I tried your code and it works fine.
Since you haven't disclosed us the variables. $file and $allowedExtensions. I assume this is what you must be doing.
Also, the if loop is just fine.
Get a new editor like PHPStorm or Eclipse for PHP
<?php
$file=array('name'=>'test.gif');
$allowedExtensions=array('.gif','.jpg','.png');
if (!in_array(array_reverse(explode(".",strtolower($file['name'])))[0],$allowedExtensions))
{
echo "Valid File Extension";
}
else
{
echo "Invalid File Extension";
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm a newbie with PCRE in PHP. I'm trying to make a very basic shortcode function that could make something with a format like this one: {somealphanumericthing}
In essence I need a preg_match_all() that could find in my post these type of occurrences. I tried something like this:
$shortcode = preg_match_all('/^\b\{[a-zA-Z0-9_]+(\}\b)$/', $body, $found);
var_dump($shortcode);
if($shortcode==1) {
for($i=0;$i<count($found);$i++) {
print_r($found);
//do something nice
}
}
But unfortunately it's not working: I get int 0 to the test string {test}
A few things about the regular expression:
You don't need your line anchors since you are searching in a larger string.
There's not need to capture the closing }
Optimization, use the character class \w
Condensed:
/\b\{[a-zA-Z0-9_]+\}\b/