Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get type in php return "object" and not the object type

Tags:

php

im trying to get the type of the object that i receive in the constructor using gettype($this->object) . but im only getting "object" my constructor: p

public function __construct($object=null)
    {
        $this->object=$object;

    }

the object that i send to class:

$campaign = new Campaign();

$type = new Nodes\CampaignDatabaseNode($campaign);
$type->checkType();

the checkType(); only echo the type of the object

like image 546
Bakalash Avatar asked Nov 11 '15 13:11

Bakalash


People also ask

How check variable is object or not in PHP?

The is_object() function checks whether a variable is an object. This function returns true (1) if the variable is an object, otherwise it returns false/nothing.

Is stdClass an object PHP?

The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.

How do you determine the datatype of a variable in PHP?

The gettype() function returns the type of a variable.

What is get type function do in PHP?

The gettype() function is an inbuilt function in PHP which is used to get the type of a variable. It is used to check the type of existing variable. Syntax: string gettype ( $var ) Parameter: This function accepts a single parameter $var.


1 Answers

Just to explain why gettype() doesn't work as expected since others have already provided the correct answer. gettype() returns the type of variable — i.e. boolean, integer, double, string, array, object, resource, NULL or unknown type (cf. the gettype() manual link above).

In your case the variable $campaign is an object (as returned by gettype()), and that object is an instance of the class Campaign (as returned by get_class()).

like image 98
madsen Avatar answered Sep 19 '22 16:09

madsen