PHP - Static methods in call_user_func_array() - php

I have a MVC Project and I have used call_user_func_array function to pass the arguments to the specific method in the controller, but the main problem here is that the method must be static, and someTimes I do really need to use $this inside that static method which is the method of the controller :/
Here's that message which explain everything: Deprecated: Non-static method home::index() should not be called statically in [Location..]
My Question here is: is there any other way to solve that problem and work with the regular methods .. !-- Maybe other function instead of the call_user_func_array or anything else :)) -> Thanks!

You can all methods non-statically via an array as shown in the example in the manual for call_user_func_array:
<?php
$parameters = ['one', 'two'];
$class = new className;
call_user_func_array([$class, 'method'], $parameters);
class className
{
public function method($one, $two)
....

Related

Using $this when not in object context error after upgrading [duplicate]

I've got a problem:
I'm writing a new WebApp without a Framework.
In my index.php I'm using: require_once('load.php');
And in load.php I'm using require_once('class.php'); to load my class.php.
In my class.php I've got this error:
Fatal error: Using $this when not in object context in class.php on line ... (in this example it would be 11)
An example how my class.php is written:
class foobar {
public $foo;
public function __construct() {
global $foo;
$this->foo = $foo;
}
public function foobarfunc() {
return $this->foo();
}
public function foo() {
return $this->foo;
}
}
In my index.php I'm loading maybe foobarfunc() like this:
foobar::foobarfunc();
but can also be
$foobar = new foobar;
$foobar->foobarfunc();
Why is the error coming?
In my index.php I'm loading maybe
foobarfunc() like this:
foobar::foobarfunc(); // Wrong, it is not static method
but can also be
$foobar = new foobar; // correct
$foobar->foobarfunc();
You can not invoke the method this way because it is not a static method.
foobar::foobarfunc();
You should instead use:
$foobar->foobarfunc();
If however, you have created a static method something like:
static $foo; // your top variable set as static
public static function foobarfunc() {
return self::$foo;
}
then you can use this:
foobar::foobarfunc();
You are calling a non-static method :
public function foobarfunc() {
return $this->foo();
}
Using a static-call :
foobar::foobarfunc();
When using a static-call, the function will be called (even if not declared as static), but, as there is no instance of an object, there is no $this.
So :
You should not use static calls for non-static methods
Your static methods (or statically-called methods) can't use $this, which normally points to the current instance of the class, as there is no class instance when you're using static-calls.
Here, the methods of your class are using the current instance of the class, as they need to access the $foo property of the class.
This means your methods need an instance of the class -- which means they cannot be static.
This means you shouldn't use static calls : you should instanciate the class, and use the object to call the methods, like you did in your last portion of code :
$foobar = new foobar();
$foobar->foobarfunc();
For more informations, don't hesitate to read, in the PHP manual :
The Classes and Objects section
And the Static Keyword page.
Also note that you probably don't need this line in your __construct method :
global $foo;
Using the global keyword will make the $foo variable, declared outside of all functions and classes, visibile from inside that method... And you probably don't have such a $foo variable.
To access the $foo class-property, you only need to use $this->foo, like you did.
If you are invoking foobarfunc with resolution scope operator (::), then you are calling it statically, e.g. on the class level instead of the instance level, thus you are using $this when not in object context. $this does not exist in class context.
If you enable E_STRICT, PHP will raise a Notice about this:
Strict Standards:
Non-static method foobar::foobarfunc() should not be called statically
Do this instead
$fb = new foobar;
echo $fb->foobarfunc();
On a sidenote, I suggest not to use global inside your classes. If you need something from outside inside your class, pass it through the constructor. This is called Dependency Injection and it will make your code much more maintainable and less dependant on outside things.
First you understand one thing, $this inside a class denotes the current object.
That is which is you are created out side of the class to call class function or variable.
So when you are calling your class function like foobar::foobarfunc(), object is not created.
But inside that function you written return $this->foo(). Now here $this is nothing. Thats why its saying Using $this when not in object context in class.php
Solutions:
Create a object and call foobarfunc().
Call foo() using class name inside the foobarfunc().
When you call the function in a static context, $this simply doesn't exist.
You would have to use this::xyz() instead.
To find out what context you're in when a function can be called both statically and in an object instance, a good approach is outlined in this question: How to tell whether I’m static or an object?
Fast method : (new foobar())->foobarfunc();
You need to load your class replace :
foobar::foobarfunc();
by :
(new foobar())->foobarfunc();
or :
$Foobar = new foobar();
$Foobar->foobarfunc();
Or make static function to use foobar::.
class foobar {
//...
static function foobarfunc() {
return $this->foo();
}
}
It seems to me to be a bug in PHP.
The error
'Fatal error: Uncaught Error: Using $this when not in object context
in'
appears in the function using $this, but the error is that the calling function is using non-static function as a static. I.e:
Class_Name
{
function foo()
{
$this->do_something(); // The error appears there.
}
function do_something()
{
///
}
}
While the error is here:
Class_Name::foo();
$foobar = new foobar; put the class foobar in $foobar, not the object. To get the object, you need to add parenthesis: $foobar = new foobar();
Your error is simply that you call a method on a class, so there is no $this since $this only exists in objects.
Just use the Class method using this foobar->foobarfunc();

How does Laravel's "withVariableName" works?

Looking at Laravel code I found they are passing variable from 'routes' to 'views' using the following method:
$arraysan = ['mike','robert','john']; **//Variable to be passed**
return view('home')->withArraysan($arraysan); **//Variable passed with** name "withArraysan"
In that above syntax they call a function named withArraysan which doesn't exist.
Can somebody explain how its been handled in Laravel?
For a while now, PHP has had the concept of magic methods - these are special methods that can be added to a class to intercept method calls that do not exist.
It appears that Laravel Views implement __call - this then intercepts a call to an undefined method on the object, and is passed both the name of the method being called as well as the arguments. In this way, the View object can then see that the withArraysan call began with and call the concrete method with, passing the second part Arraysan as the first argument, and the argument to withArraysan as the second part.
If I've got your question then in Laravel they had a class View using magic method __call to handle the above function and the code for that function is like as follows
public function __call($method, $parameters)
{
if (Str::startsWith($method, 'with')) {
return $this->with(Str::snake(substr($method, 4)), $parameters[0]);
}
throw new BadMethodCallException("Method [$method] does not exist on view.");
}
And you can find this within
your_project_folder/vendor/laravel/framework/src/Illuminate/View/View.php
$arraysan = ['mike', 'robert', 'john']; // Variable to be passed
return view('home')->with('AnyVariable', $arraysan);
Try this! This will work.
Also check in home.blade.php,
<?php
print_r($AnyVariable);die;
?>

Is there a way to find out if a method is static or not?

Is there a way to find out if a method is static or not?
My reason for needing to know:
I call static methods outside of any instantiation context. Non-static methods can't be called then, since they don't make sense yet. I want to call them later, once instances of those classes exist.
When I call call_user_function_array($className.'::'.$functionName, $args); and the method is non-static, php seems to automatically create an instance of className and call that function. I want that call to FAIL for non-static functions.
When I call call_user_function_array($className.'::'.$functionName, $args);
and the method is non-static, php seems to automatically
create an instance of className and call that function.
No, it doesn't. PHP isn't that automagic. No idea what you're doing there.
To call a method statically, you do exactly that:
call_user_func_array("$className::$functionName", $args);
To call a method of an object, you first need to explicitly instantiate an object, then call it like this:
$obj = new MyClass;
call_user_func_array(array($obj, $method), $args);
To programmatically figure out if a method is static or not, use ReflectionClass:
$r = new ReflectionClass($myClass);
$m = $r->getMethod($method);
var_dump($m->isStatic());
You should really know what a method is before you call it though, instead of dynamically trying to figure it out.
You can check methods with reflection.
class foo
{
static public function bar()
{}
public function baz()
{}
}
$reflection_class = new ReflectionClass('foo');
var_dump($reflection_class->getMethod('bar')->isStatic()); // boolean true
var_dump($reflection_class->getMethod('baz')->isStatic()); // boolean false
P.S. It's very weird that you trying to call methods but you do not know what they really are.
Since reflection is expensive I actually ended up calling set_error_handler with a callback that throws an ErrorException as described here to catch the warning when a static call was made to a non-static method.

php static methods question

What is the difference between these two pieces of code?
class something {
static function doit() {
echo 'hello world';
}
}
something::doit();
and the same but without the static keyword
class something {
function doit() {
echo 'hello world';
}
}
something::doit();
They both work the same is it better to use the static keywords? Am i right in understanding that it doesn't instantiate the class if you use the static method?
The second example is technically incorrect - if you turn on E_STRICT error reporting you'll see that PHP is actually throwing an error.
PHP Strict Standards: Non-static
method something::doit() should not be
called statically in...
In other words, it's being nice and letting you call the function anyway.
In addition to the other valid answers, the reason for the 2nd example working is also due to a quirk in how PHP handles objects and calls (Besides PHP 4 compatibility). Calling a non-static declared method statically from within another instance will let you access class methods on other classes as if they were local. To understand, let's take an example:
class A {
public function foo() {
echo get_class($this) . "\n";
}
}
class B {
public function bar() {
A::foo();
}
}
$a = new a();
$a->foo(); // "A"
$b = new B();
$b->bar(); // "B"
Did you see what happened there? Because you called the A::foo() method from within another class's instance, PHP treated the call as if it was on the same instance. Note that there is no relationship between A and B other than the fact that B calls A. Within A->foo(), if we did $this instanceof A (or $this instanceof self), it would fail and return false! Quite unusual...
Now, I first thought it was a bug, but after reporting it, it's apparently as designed. It's even in the docs.
Note that this will not work with E_STRICT mode enabled. It also will not work if you declare a method as static.
The difference is that static functions can be used without having to create an instance of the class.
Have a look at this great PHP OOP beginner tutorial here. It explains in more detail with an example under the Static Properties and Methods section.
Second bit shouldn't work as you should call it by
$something = new something();
$something->doit();
Static functions allows you to call a function within a class without consturcting it.
So basically if you have a class to handle users, so a function that logs the user in should be a static function, as in the constructor of that class you will probably gather the user information and you cannot do so without logging him in.
Your second example is wrong. Using a static method does not create an instance of the class. Your second example should look like this:
$x = new something();
$x->doit();
Static methods should be declared static for minimum two reasons:
a) when using E_STRICT error_reporting, calling non static method as static will generate error:
Strict standards: Non-static method something::doit() should not be called statically
b) based on keyword static some IDE's filter method possible to run at auto-complete.

PHP5 Class scope quirks

Hey php gurus. I'm running into some bizarre class scope problems that clearly have to do with some quirk in php. Can anyone tell me what out-of-the-ordinary situations might give the following error...
Fatal error: Cannot access self:: when no class scope is active in MyClass.php on line 5
Now, obviously if I were to use self:: outside of the class, I'd get errors... but I'm not. Here is a simplified version of the situation...
//file1
class MyClass{
public static function search($args=array()){
$results = MyDbObject::getQueryResults("some query");
$ordered_results = self::stack($results); //Error occurs here
return $ordered_results;
}
public static function stack($args){
//Sort the results
return $ordered_results;
}
}
//file 2
include_once("MyClass.php");
$args = array('search_term'=>"Jimmy Hoffa");
$results = MyClass::search($args);
given this setup how can I get the error above? Here is what I've found so far...
MyClass::search($args) //does not give the error (usually)
call_user_func("MyClass::search"); // this gives the error!
Any other situations?
If I understand correctly, you are looking for Late Static Binding. This feature requires PHP version 5.3 at least.
You're not passing any parameters, but your method is looking for them. Try
call_user_func("MyClass::search", $args);
This works in php 5.3.1, but call_user_func("MyClass::search"); doesn't
Try this:
call_user_func(array('MyClass', 'search'));
See also example #4 on http://php.net/call_user_func
Your code seems fine. If there's something wrong with it, I must be missing the problem. It appears that your call to self:: is totally within the scope of a class! And a static scope, specifically, which is what self:: is for.
From the 3rd Edition of PHP Objects Patterns and Practice (an awesome book):
To access a static method or property from within the same class
(rather than from a child), I would use the self keyword. self is to
classes what the $this pseudo-variable is to objects. So from outside
the StaticExample class, I access the $aNum property using its class
name:
StaticExample::$aNum;
From within the StaticExample class I can use the self keyword:
class StaticExample {`
static public $aNum = 0;
static public function sayHello() {
self::$aNum++;
print "hello (".self::$aNum.")\n";
}
}
So, I am not sure why this code was failing. Perhaps a PHP bug? I came upon this error when actually trying to use self:: outside of the scope of a class-- my error looked like this:
public static function get_names() {
$machine_names = self::get_machine_names();
return array_map(function ($machine_name) {
$service_settings = self::get_settings_by_machine_name($machine_name);
return $service_settings . $machine_name;
},
$machine_names
);
}
So, I get the error because I use self:: within the scope of the closure. To fix the error, I could make that call to self::get_settings_by_machine_name() before the closure, and pass the results to the closure's scope with use.
Not sure what was happening in your code.

Categories