PHP get class reference of an object - php

for instance
I've a class called Ent, and have an object $ent
how can I get the class Ent out of $ent?
$ent = new Ent()

get_class(new Ent())
this will even work in earlier versions of PHP

You can use get_class($ent)
get_class - Returns the name of the class of an object
Reference php docs

You could get get_class().
Watch out though, if your calling it on a variable that might not be an object it will throw a warning.
if(is_object($ent))
{
$class = get_class($ent);
}
Ref: http://php.net/manual/en/function.get-class.php

$ent = new Ent();
$my_class = get_class($ent);
$new_ent = new $my_class;
RTM: Here

Related

How new object working in the latest version of PHP

As per the PHP 7.2 documentation
A new type, object, has been introduced that can be used for (contravariant) parameter typing and (covariant) return typing of any objects.
And the following example has been given
<?php
function test(object $obj) : object
{
return new SplQueue();
}
test(new StdClass());
Can someone elaborate what is meant by contravariant parameter and covariant return type and how this new object work
object in both places in your code can return generic object i.e an instance of any type. (as shown in your example)
Else it would need to be:
<?php
function test(StdClass $obj) : SplQueue
{
return new SplQueue();
}
test(new StdClass());

Can I use a string variable to initialize a class in PHP?

I'd like to use a string variable to initialize an object. Is something like this possible?
$class = "MyClass";
$x = new $class();
return $x;
Edit: Ha, so when I tried to test this and it didn't work I had a syntax error somewhere else in my script. Apparently this works just fine. Neat.
Yes. Its possible in PHP.
$className = 'MyClass';
$object = new $className;
Attaching PHP documentation snippet on new operator

How can I use a variable as the name of object's method?

Here is my code:
$method_name = 'mymethod()';
$obj = new Myclass();
$obj->$method_name;
As you see I've used $method_name as the name of a method. But it throws this error message:
Undefined property: app\classes\Myclass::$mymethod()
How can I fix it?
You should avoid using string to do reflection... And use the ReflectionClass and the ReflectionMethod.
However, the proper way of doing it is:
$method_name = 'mymethod';
$obj = new Myclass();
$obj->$method_name();
You have to use callback function call_user_func. To do this You need to make an array:
The 1st element is the object
2nd is the method
call_user_func(array($player, 'doIt'));
You can also do it without call_user_func:
$player->{'SayHi'}();
Or:
$method = 'doIt';
$player->$method();
You set method name to be mymethod(), that is invalid.
Set it just to mymethod:
$method_name = 'mymethod';
$obj = new Myclass();
$obj->{$method_name}();

Laravel 5.1 GeoIP Non-static method should not be called statically, assuming $this from incompatible context

I am using Torann\GeoIP And I am getting this error when I try
use Torann\GeoIP\GeoIP;
Route::get('geoip', function() {
$location = GeoIP::getLocation();
});
but when I try with
$geo = new GeoIP();
$geo - getLogation();
I have this error 'Argument 1 passed to Torann\GeoIP\GeoIP::__construct() must be an instance of Illuminate\Config\Repository, none given'
so I am missing the arguments for the __construct ....$config, $session
so it should be looking like this
$loc = new GeoIP($config, $session);
$loc ->getLocation();
but what do I need to give to $config = ? and $session = ?
Any siggestions will be helpfull. Thank you
If there is better way to get the GeoLocation data it would be great.
You must declare:
use Torann\GeoIP\GeoIPFacade as GeoIP;
and move the file
/vendor/torann/geoip/src/config/geoip.php into /config/geoip.php.

Weird problem with dynamic method invocation

this time, I'm facing a really weird problem. I've the following code:
$xml = simplexml_load_file($this->interception_file);
foreach($xml->children() as $class) {
$path = str_replace('__CLASS_DIR__',CLASS_DIR,$class['path']);
if(!is_file($path)) {
throw new Exception('Bad configuration: file '.$path.' not found');
}
$className = pathinfo($path,PATHINFO_FILENAME);
foreach($class as $method) {
$method_name = $method['name'];
$obj = new $className();
var_dump(in_array($method_name,get_class_methods($className)));exit;
echo $obj->$method_name();### not a method ???
}
}
As you can see, I get the class name and method name from an XML file.
I can create an instance of the class without any problem. The var_dump at the end returns true, that means $method_name (which has 2 optional parameters) is a method of $className.
BUT, and I am pretty sure the syntax is correct, when I try: $obj->$method_name() I get:
Fatal error: Method name must be a string
If you have any ideas, pleaaaaase tell me :)
Thanks in advance,
Rolf
The issue you are having is probably that $method_name is not a string, but it contains a method to convert it to a string (__toString()).
As in_array by default don't do strict type comparisons you will find that $method_name is probably coveted to a string and then compared with the method names, which would explain why the var_dump outputs true.
You should be able to confirm this by checking the type of $method_name
echo gettype($method_name);
If it isn't a string the solution is to case the variable to a string and then use that to call the function.
$obj->{(string)$method_name}();
It's better to use the call_user_func function instead of $obj->$method_name() to call the method.
echo call_user_func(array($className, $method_name));

Categories