How non static method working statically on localhost - php

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);

Related

Error with __Call on magento

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

PHP - Calling a static function from an object instance works?

I just wrote a sample class to better understand the static methods and variables in PHP. I understand how the static variables work but the static function is not working as expected. If you see the below code
class Car{
static $wheels=4;
static function getWheels(){
echo Car::$wheels=10;
}
}
$car1 = new Car();
$car1->getWheels();
I was expecting
$car1->getWheels(); to throw and error since getWheels is a static method.
Why is this not throwing an error or warning?
I think it comes from the PHP 4 times, where there was no static keyword but you could call static methods whether with the -> or the :: operator
In fact, tecnically speaking, calling $car1->getWheels() was (and is) translated by PHP to Car::getWheels() at run time
With the advent of PHP5 this option was mantained for backward compatibility purposes
If you enable E_STRICT error reporting though, this should raise a warning now

how to set_error_handler so it can take effect only in an instance of a class and not globally?

consider this code:
class TestClass {
function __construct() {
set_error_handler(array($this,'error_handler'));
}
function error_handler($errno,$errstr,$errfile,$errline,$errcontext) {
trigger_error("\ncode: [$errno]\nfile: [$errfile -> $errline]\nSTR: [$errstr]",E_USER_ERROR);
}
}
count($nonExistentVariable);
//notice was thrown above but script carry on
//but here error handler is registered and...
$anInstance = new TestClass();
// rest of the code is affected by it
count($nonExistentVariable); // user error is thrown
So is there a way to make the error_handler function to fire only when error is encountered inside an instance and do not mess globally with other code ?
You could check the call stack in your error handler (via debug_backtrace()) and determine if it's coming from within the class you're interested in. If it's not coming from the class be sure to return FALSE and PHP will fall back on it's default handler.
http://php.net/manual/en/function.set-error-handler.php

PHP script stops when a member function is called, no error thrown

What can cause php script to stop executing without outputting any error?
It happens to me when I call an inherited function.
Example code:
class X extends FPDF {
function foo() {
return $this->GetClientWidth();
}
}
FPDF class as in here: (deleted link) wrong version
The object is initiated, anytime I call GetClientWidth() I get a blank page
Used to work before reinstalling my OS/dev environment. Running on XAMPP
UPDATE: Sorry for confusing you, I didn't check the link I gave, it indeed did not have the function discussed. It's there though on my local version.
error_reporting(E_ALL);
ini_set('display_errors',1);
or check error.log
require_once "FPDF.php";
error_reporting(E_ALL);
ini_set('display_errors',1);
class X extends FPDF {
function foo() {
return $this->GetClientWidth();
}
}
$x = new X;
$ClienWidth = $x->foo();
var_dump($ClienWidth);
return:
Fatal error: Call to undefined method X::GetClientWidth()
The reason is simple: "There is no GetClientWidth method in the fpdf – Prisoner"
Add the following code right at the beginning of your script and check if any error is displayed.
error_reporting(E_ALL);
ini_set('display_errors', true);
when it happens to me is because the method does not exist.
I've revised your object FPDF an it's not defined

Php code not executing - dies out when trying to refer to member of static class - no error displayed

I'm having some problems with this piece of code. I've included a class declaration and trying to create an object of that class but my code dies out. It doesn't seem to be an include issue as all the files are being included even the files called for inclusion within the class file itself.
However the object is not created - I tried to put an echo statement in the __construct function but nothing it just doesn't run infact doesn't create the object and the code won't continue from there - plus no error is reported or displayed and I have error reporting set to E_ALL and display errors set to true
WHats happening here :(
=============EDIT
SOrry I checked again the error is prior to teh object creation thing - it dies out when it tries to refer to a constant in a static class like so:
$v = Zend_Oauth::REQUEST_SCHEME_HEADER;
THis is the class or part of it - it has largely static functions its the Zend Oauth class:
class Zend_Oauth
{
const REQUEST_SCHEME_HEADER = 'header';
const REQUEST_SCHEME_POSTBODY = 'postbody';
const REQUEST_SCHEME_QUERYSTRING = 'querystring'; // continued
LIke I said no error is being reported at all :(
If php really bails out on $v = Zend_Oauth::REQUEST_SCHEME_HEADER; then it's most likely a
PHP Fatal error: Class 'Zend_Oauth' not found
error. But that's something you would see with error_reporting(E_ALL);
Other than that there's nothing in that line of code that can cause a fatal error.
If you want to put some debug (output) code in there try something like
echo "<h1>Debug: A</h1>\n"; flush();
error_reporting(E_ALL); ini_set('display_errors', 1);
$v = Zend_Oauth::REQUEST_SCHEME_HEADER;
echo "<h1>Debug: B</h1>\n"; flush();

Categories