instantiating a object on the fly a static method? - php

I'm not so sure what the following object instantiation is called but it comes from a article that i'm reading.
class foo
{
function out()
{
return 'hello';
}
}
echo (new foo())->out();
The object is instantiated automatically and calls the out method. But what i dont really understand is when i rename the out() method into a fictitious method i get an error like this:
example:
class foo
{
function out()
{
return 'hello';
}
}
echo (new foo())->ou();
Fatal error: Call to undefined method foo::ou() in ...
Is this method somehow being called as a static method?

The :: does not stand for static method, this is a missconception. The :: is a "scope resolution operator", it denotes the identification of a method by its class predicated full name.
So this simply means: "method 'ou' as defined by class 'foo'". Not more, not less.

No. The error just indicates that the method doesn't exist. It always shows the :: for this error, no matter whether you call the method in a static way or not. You would get the same error if you changed the code to:
$foo = new foo();
echo $foo->ou();
Second code example as per request in comments:
$moo = new moo(); // Parentheses optional, I guess
$foo = new foo($moo);
$foo->out();

Related

Pass object function as callback

are there ways to pass a "method reference" to a function that accept a callback?
Eg.:
function ex($callback){
$callback();
}
$obj = (object) ['f' => function(){echo "hello";}];
ex($obj->a)
Obviously this is a very simplistic case, image having a complex object $obj with parameters inside
PHP does not work like Javascript.
This code:
$obj = (object) ['f' => function(){echo "hello";}];
Does not define "an object with method f()" it defines "an object with property f that happens to be a callable type".
Calling $obj->f(); directly results in:
Fatal error: Uncaught Error: Call to undefined method stdClass::f()
It would need to be specially handled like:
$func = $obj->f;
$func();
Or:
($obj->f)();
While not a wholly accurate statement, you could consider it that callable types need the equivalent of "being dereferenced" before they can actually be called.
You example is something of a "successful error" in that it attempts to use "first class functions" which PHP does not have, but on a malformed object that allows this to not fail.
Using a proper class:
function ex($callback){
$callback();
}
class Example {
public function f_instance() {
echo "instance call\n";
}
public static function f_static() {
echo "static call\n";
}
}
ex(['Example', 'f_static']);
$e = new Example();
ex([$e, 'f_instance']);
Output:
static call
instance call
Ref: https://www.php.net/manual/en/language.types.callable.php

callback invoked in class extension

I'm working my way through php callbacks. I think I've covered the basics, but when working within class extensions I'm still hitting a brick wall somewhere for some time now, therefore it is time for some stack overflow rubber duck programming...!
Calling a callback from a class extension:
class myClass {
function cb_check($function) {
call_user_func($this->$function);
}
static function myFunction() {
var_dump("Hello World");
}
}
class myExtClass extends myClass {
function cb_invoke() {
$this->cb_check('myFunction');
}
}
$x = new myExtClass;
$x->cb_invoke();
Error message (or, notice and warning):
Notice: Undefined property: myExtClass::$myFunction in F:\test.php on
line 5
Warning: call_user_func() expects parameter 1 to be a valid callback,
no array or string given in F:\test.php on line 5
Line 5 is the call_user_func() above.
Does anybody have an idea what I am missing here?
Thank you in advance!
As mark pointed out,
call_user_func($this->$function);
should be replaced by either
call_user_func(array($this, $function));
or
call_user_func('self::' . $function);
Thanks, Mark!
While working with static classes you should have to use scope resolution operator :: you can not create the instance directly .
$x::myExtClass;
Try it.
Your error arises because you've declared myFunction() to be static. Static methods don't belong to any specific instance of a class and therefore can't be accessed through the $this variable. Instead you use self::myFunction() to call the static method. The easiest way to make this happen is to build the string of the static method call and pass the string as the parameter to call_user_func(). Your code would work as-written if myFunction() were not static.
class myClass {
function cb_check($function) {
// Build a static-method-style string
call_user_func('self::' . $function);
}
static function myFunction() {
var_dump("Hello World");
}
}
class myExtClass extends myClass {
function cb_invoke() {
$this->cb_check('myFunction');
}
}
$x = new myExtClass;
$x->cb_invoke();

Declaring an anonymous function within new stdClass

Just wondering why something like this doesn't work:
public function address($name){
if(!isset($this->addresses[$name])){
$address = new stdClass();
$address->city = function($class = '', $style = ''){
return $class;
};
$this->addresses[$name] = $address;
}
return $this->addresses[$name];
}
Calling it like echo $class->address('name')->city('Class') should just echo Class, however I get Fatal error: Call to undefined method stdClass::city()
I can find a better way to do this, because this will get messy, but I'm wondering what I might be doing wrong there, or if PHP doesn't support this and why.
PHP is right when invoke fatal error Call to undefined method stdClass::city() because object $class->address('name') has no method city.
Intead, this object has property city which is instance of Closure Class (http://www.php.net/manual/en/class.closure.php)
You can verify this: var_dump($class->address('name')->city)
I found the way to call this anonymous function is:
$closure = $class->address('name')->city;
$closure('class');
Hope this helps!
Sadly it is not possible within stdClass, but there is a workaround -- PHP Anonymous Object.
// define by passing in constructor
$anonim_obj = new AnObj(array(
"foo" => function() { echo "foo"; },
"bar" => function($bar) { echo $bar; }
));
$anonim_obj->foo(); // prints "foo"
$anonim_obj->bar("hello, world"); // prints "hello, world"
AFAIK, this is not supported by PHP, and you must use the call_user_func() or call_user_func_array() functions to call closures assigned to class properties (usually you can use __call() to do this, but in your case, the class is stdClass, so this isn't possible).

PHP: How many of these lines don't work, static vs non-static access

http://codepad.viper-7.com/ezvlkQ
So, I'm trying to figure out:
...?php
$object = new A();
class A
{
static public $foo = 'bar';
function displayFoo()
{
echo $this->$foo;
}
}
A::displayFoo();
A->displayFoo();
?>
About this, how many errors can you find? Can you tell me what they are in real human terms? I can't really interpret what is and what is not okay from the validator that codepad uses...
I’ve updated your code here http://codepad.viper-7.com/UaUE4g
Error 1:
echo $this->$foo;
This should read:
echo self::$foo;
.. as it is static.
Error 2:
A::displayFoo();
The method is an instance method :: is used for access to static methods.
Error 3:
A->displayFoo();
This is an error because A is undefined and if it was it should have read $A. This would be okay:
$object->displayFoo();
.. as $object is an instance of class A.
Next step, consult the manual on the topic static.
Not sure where to start. Static methods belong to the class, normal methods belong to an object, an instantiation of that class. For example, you can have:
Class A {
static public $foo = 'WOOHOOO';
static function displayFoo() {
echo self::$foo;
}
}
echo A::displayFoo();
This works because you're calling the displayFoo method belonging to class A. Or you can do this:
Class A {
public $foo = "WOOHOO";
public function displayFoo() {
echo $this->foo;
}
}
$obj = new A();
$obj->displayFoo();
Now you're creating an object based on the class of A. That object can call its methods. But the object doesn't have static methods. If you were to declare the function static, it would not be available to $obj.
You can't do:
A->displayFoo()
at all, under any circumstances, ever. The -> operator assumes an object, and A can't be an object because its not a variable.
You can read up on static class members in the manual here:
http://php.net/static
Pay close attention to the examples.

Difference between access class methods

Up until the yesterday I though that both of these methods for accessing a class where identical. Google turned up noting (either that or my keywords were bad). What is the difference between accessing a class by defining a new instance of the class rather than just calling the class methods directly.
I hardly understood what I just ask, since I have not a clue what I'm asking but allow me to give a few examples to help out.
For as long as I have been using classes in PHP I have been defining a instance of the class then accessing the methods from that variable. Here is an example:
<?php
class Myclass
{
public function my_method() {
return "This is my method";
}
}
$myclass = new Myclass();
echo $myclass->my_method(); // output: This is my method
?>
What is the difference between that and doing this?
<?php
class Myclass
{
public function my_method() {
return "This is my method";
}
}
echo Myclass::my_method(); // output: This is my method
?>
I hope I was clear enough. If not a brief synopsis would be - What is the difference between creating a new instance of a class then calling a method of the class with "$class->mymethod();," or just directly calling the method like so: "Myclass::mymethod();"?
Thanks in advance.
The :: notation is used to call the function as static. See these pages from the PHP manual for more info:
http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php
http://php.net/manual/en/language.oop5.static.php.
In essence, static functions are just like regular functions outside classes. The function is not in object context when called as such, so the pseudo-variable $this is not available.
Note that the function should be declared as static if you are planning on calling it as such:
class SomeClass {
static public function StaticFunction() {
...
}
}
The Myclass::my_method() call should be throwing an E_STRICT warning, as you're calling a non-static method in a static context.
Static methods are methods that belong to the class rather than to the instance. These methods cannot access any non-static variables, as they have no reference for them (because non-static variables belong to the instance, not the class).
After using Java and C#, I'm mildly disturbed that this isn't considered an E_WARNING or E_ERROR, particularly since E_STRICT is not logged by default.
First, you should understand the difference between a class and an object, or a class and an instance of a class.
For example, where class is "Circle", an instance might be "this particular circle". Class Circle might have members $center and $radius - every point has these. But the values of these are different for every point.
$class->mymethod() is a call to an instance method, for instance $circle->Length(). You are saying something to a particular instance of an object. This method will have access to class members - variables that belong to the instance. In PHP you gain access to these instance variables using keyword $this.
For instance $circle->Length() might be implemented as return 2*Pi*$this->radius;
What happens when you say Circle::Length()? Nothing. This is syntax for calling static methods. Because you did not say which circle. Circle as a class does not have length.
Static method is declared with keyword static. Whatever it does it has no access to any instances of the class. It's something that a class as a whole does.
For instance, if you want to calculate length of the circle without actually creating one, then there might be static function calculateLength($radius) in the class circle. And you would call it as Circle::calculateLength($myRadius)
In my opinion you should not even think about static methods until you understand the non-static ones.
http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php
I don't think you can call methods that way. I thought that was how you called static members of the class.
In my understanding when you create an instance of the class you are creating an object and so you call methods/properties from that object.
When you call a method/property via a static call you are calling on the class itself.
So if you have something like this:
class acs {
public $var = 'bla';
}
and then you do
$a = new acs();
$b = new acs();
echo $a->var . '<br>';
echo $b->var . '<br>';
$a->var = 'hey';
$b->var = 'Yooo';
echo $a->var . '<br>';
echo $b->var . '<br>';
You now have 2 instances of class acs which started with the same values but now have different values.
But if you have this:
class acs {
public static $var = 'bla';
}
And then do:
$a = new acs();
$b = new acs();
echo $a->var . '<br>';
echo $b->var . '<br>';
$a->var = 'hey';
$b->var = 'Yooo';
echo $a->var . '<br>';
echo $b->var . '<br>';
echo acs::$var;
What you are doing is:
You create the instances as before.
You echo nothing, because the public property var does not exist.
You create the public property var in both the instances
You echo their values (hey and Yoo)
You echo the static property var (which is 'bla' and has not changed)
Hope this helps!

Categories