This question already has answers here:
In PHP, can you instantiate an object and call a method on the same line?
(9 answers)
Closed 8 years ago.
I've an object which returns a string and I would like instanciate another object with the string returned, why in PHP I can't instanciate this way ?
For Example:
// getController() returns a string name controller
$c = new $this->router->getController() ;
// I have to do this way:
$controller = $this->router->getController() ;
$c = new $controller() ;
Thank you for your help.
It's a syntax thing. I think I remember seeing an RFC to allow more dynamic class instantiation, but assigning to a variable first gets the job done, no?
Related
This question already has answers here:
Add method in an std object in php
(5 answers)
PHP override function of a single instance
(2 answers)
Closed 7 months ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
How add function to instance object of stdClass?
$obj = (object)['name' => 'test', 'x' => 0];
$obj->move = function() { $this->x++; return $this->x; };
var_dump(($obj->move)());
I try use closure to simulate method, there is another way to directly add dynamicly a function to my object?
For my exemple, there is an error :
Fatal error: Uncaught Error: Using $this when not in object context
so how keep object context, for use $this in my closure ?
I try add use($obj) but error stay... (normal)
So how can I do ?
This question already has answers here:
How can I call a static method from a class if all I have is a string of the class name?
(7 answers)
Closed 1 year ago.
I have the string with class name. I want to get a class with this name and call a static methods of this class.
How to make something like this?
$className = "Model";
getClassByName($className)::sayHello();
You can simply call the method on the variable, you might want to wrap an "if" around it to check if the class exists.
$className = "Model";
if (class_exists($className)) {
$className::sayHello();
}
You can check out this 3v4l for a repro case.
This question already has answers here:
What does it mean to start a PHP function with an ampersand?
(3 answers)
Closed 7 years ago.
I'm using a CMS package written in PHP. In one of it's core files I saw following line that is for defining a function in a class body.
public static function &getLib($sClass, $aParams = array()) {
// Code
}
I didn't understand why the function name 'getLib' has been prepended with the ampersand(&) sign? I've never seen such thing before.
Can someone please explain me in detail why such thing has been done and what's the benefit it has over simply using the function name?
It means the function should return a reference to a variable rather than just the value itself.
This question already has answers here:
Instantiate a class with or without parentheses? [duplicate]
(3 answers)
Closed 9 years ago.
I'm learning the advance concepts of php. What does new stdClass do? I know what new stdClass(); does.
For eg) <?php new stdClass(); ? > creates a new object. Does <?php new stdClass; ? > do the same thing? Notice there's no parentheses. Does that make a difference? I can't find documentation on it for php manual.
There is no difference. PHP lets you omit the parentheses if you're not passing arguments to the constructor.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
declare property as object?
in java you can create an object directly after the property field like this:
but it seems not working for php:
class Test {
public $object = new Object();
}
you have to create it in the __construct() and assign it to the property?
thanks
From php.net
This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
So no, you cant initialize it to an object. You'll have to do it in the constructor like you said