How to get class by name in PHP? [duplicate] - php

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.

Related

Define properties of a class [duplicate]

This question already has answers here:
How can I access an object property named as a variable in php?
(5 answers)
Closed 2 years ago.
Is there any way to define the properties of a class generic? To be more specific can I have let's say a file with define statement e.g.
define('USER_ID', 'userId')
and a class
foo{$userId}
How can I access the property like
$foo->USER_ID
?
Is there any way to achieve something like the above?
Thanks
You should use class constant :
<?php
class Demo {
const USER_ID = 3;
}
echo Demo::USER_ID;
?>

PHP __CLASS__ like magic constant respect inheritance? [duplicate]

This question already has answers here:
How to get the name of child class from base class when an object of child class is created
(4 answers)
Closed 7 years ago.
Is there a magic constant which, when used in a parent class, will return its child's class name, and not the parent?
What you need is a late static binding.
Not sure if you need a constant only. But using get_class($this); in parent you will get the class of the caller(parent or child)

PHP dynamic instanciating [duplicate]

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?

finding the name of method i am in [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
get current class and method?
How can i find the name of the method i am using in php? I found how to do this in C but not in PHP. I found a Q on here which roughly talked about magic constants (here) but I didn't really get it. In the following example I want $thisMethodName to be 'model_databaseLogin'
EG:
public function model_databaseLogin()
{
$thisMethodName = ... ;
return $this->model_methodCheck( $thisMethodName );
}
Is this possible in php?
You need the "magic constant" __METHOD__. The magic constant docs should be helpful.
So your code would be:
public function model_databaseLogin() {
$thisMethodName = __METHOD__;
return $this->model_methodCheck($thisMethodName);
}
The simplest answer is the magic constants to which you refer; specifically __FUNCTION__
These are called "magic" because their value is actually contextually dynamic.
public function model_databaseLogin()
{
$thisMethodName = __FUNCTION__;
return $this->model_methodCheck( $thisMethodName );
}
There is another way, via debug_backtrace(), but that is decidedly less efficient!

create class directly after a property field? [duplicate]

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

Categories