Error with __Call on magento - php

I can't figure this out,
I have a method on my class like:
public function __call($closure, $args){
return call_user_func_array($this->{$closure}->bindTo($this),$args);
}
This works on my local server on magento, but when I try to use it on my server
it returns me the following error:
Fatal error: Call to undefined method Closure::bindTo() in
I tried adding var_dump to the variables, all have the correct values.
Any ideas?

Closure::bindTo exists in PHP 5.4 or newer only. Check your PHP version.
http://php.net/manual/en/closure.bindto.php

Related

How non static method working statically on localhost

I made a class and in that class made a public method. now am calling that method as static method and its working fine. How?
if I upload this code on PHP fiddle its giving error which I was expecting. But why it's not giving error on localhost
class A
{
public function b()
{
echo "i am b";
}
}
print_r(A::b());
Statical call of a non-statical method is deprecated since 5.6 version of PHP. It means if you call a non-statical method like statical you get an error of E_DEPRECATED level.
If you don't see this error you should reconfigure error reporting and enable display errors.
For example:
ini_set('display_errors', 1);
error_reporting(E_ALL);

Function echoing causes error

I have made a public function for getting, and showing a user's infraction info. When I put it on a page, it only shows what I have in the function, and not any of the other content on the page. It only shows the table headers, and none of the data. I get this error:
Fatal error: Call to a member function query() on a non-object in /Applications/AMPPS/www/classes/user.php on line 108
Also, I have other functions from the same class that work fine.
Here is the code for the function link (sorry about pastebining it, it was really long)
Your $db object is null or can't be accessed. Your line 108 error does not match up with your code you have pasted and you don't have the code where you are creating your database object to see what may be wrong there.
The error message seems to indicate that your "$db" is not set to an object. Make sure its initialized properly.
The function never initializes the $db variable. If it's a class property, it should be $this->db or self::$db. If it's a global variable, you need to put global $db; at the beginning of the function.

Class method chaining

I have the following code but it does not seem to want to work chained.
$this->view->setData($class_vars);
$this->view->render('addview');
The above works and runs fine but when i try to do the following:
$this->view->setData($class_vars)->render('addview');
I get the following error:
Fatal error: Call to a member function render() on a non-object in....
But the strange thing is when i call it the other way:
$this->view->render('addview')->setData($class_vars);
It runs, but I need the setData to run first as this sets up the var for the actual view, so even though i get the view its got errors where the vars should be? Both methods are public?
Thanks You
Does setData() return the view object (i.e. it has return $this; line)? If not... well it should if you want it to work this way.
For further reference. This technique is called 'fluent interface' and is described here:
http://www.martinfowler.com/bliki/FluentInterface.html

How to tell when a PHP Fatal Error is really a veiled Syntax Error, and when do they happen?

Considering the following PHP class:
class someObject {
public function broken(){
return isset($this->something()) ? 'worked' : 'didnt';
}
public function something(){
return true;
}
public function notBroken(){
print('worked');
}
}
Let's say I now do:
$obj= new someObject();
$obj->broken();
Considering you can't pass a function call to isset(), (it's by-reference), I expect this to fail with a fatal error: PHP Fatal error: Can't use method return value in write context This is fine, and expected.
However, let's say I now do:
$obj= new someObject();
$obj->notBroken();
Considering I'm not hitting the broken() anywhere in this execution, and the error in broken() is a Fatal Error (and not a Parse error), I wouldn't expect the normal output of "worked". FALSE! It still generates the Fatal Error.
Question:
Aside from just not writing code that has errors, are there any other errors that are not Parse Errors but still trigger a runtime error? I only know about: PHP Fatal error: Can't use method return value in write context. Is there any way to detect these errors?
Is there a special name for this type of error?
The reason for this specific behaviour is probably that isset() is a language construct and not a normal function that gets interpreted at runtime. So it stands to reason this is kind of a parse error.
I have no deep insight in this though, and I don't know whether this class of errors has a specific name.
These are "compile errors", thrown by the compiler when it encounters a syntactically valid but "uncompilable" construct. Go to http://svn.php.net/viewvc/php/php-src/trunk/Zend/zend_compile.c and search for "E_COMPILE_ERROR" - there are quite a few.

Fatal error: Call to undefined method stdClass

I get an error that says
Fatal error: Call to undefined method stdClass::mysql_con() in ........../.../includes/script/import.php on line 68.
Line 68 corresponds to:
if(!$ip2c->mysql_con())
I do have a require_once() statement at the beginning of my script
What could be the problem here?
Thanks
Dusoft says it could mean:
$ip2c object does not exist,
Which is not correct because you would get a different error "Fatal error: Call to a member function mysql_con() on a non-object"
He also says it could mean:
mysql_con function is not part of the class you are trying to call
Which is true but not so helpful cos its very difficult to add methods to stdClass.
Additionally it could be to do with serialisation quote:
This error is normally thrown when a class instance has been serialised to disk, then re-read/deserialised in another request but the class definition has not been loaded yet, so PHP creates it as an "stdClass" (standard class.)
Or most likely, I think:
the $ip2c variable was not an object and then php silently cast it to become stdClass somewhere in the code above.
This could happen if you directly assign a property on it.
Like:
$ip2c = null;
//php casts $ip2c to 'stdClass'
$ip2c->foo = bah;
//Fatal error: Call to undefined method stdClass::mysql_con() in...
$ip2c->mysql_con();
See a better example here.
it means that either $ip2c object does not exist or mysql_con function is not part of the class you are trying to call.
I think this happen because "extension=php_mysql.dll" extension isn't loaded in php.ini.
Take a look with
phpinfo();
It could be incorrect code. I once managed to get that error when I had this line of code:
if ($myObj->property_exists('min')){
// do something
}
Which resulted in error line like this:
PHP Fatal error: Call to undefined method stdClass::property_exists() in myFile.php on line ###
I later fixed the line to:
if (property_exists($myObj, 'min')) {
// do something
}
So check for that possibility as well.
Most likely the object does not exist. Please show us the code of how you created it. If you are using it within another class (maybe creating it in the __construct function for example), using:
$ip2c = new Class;
Won't cut it. Instead do:
$this->ip2c = new Class;
and then
$this->ip2c->mysql_con();

Categories