june8
june8

Reputation: 699

php stdClass check for property exist

I have read some similar issues here but unfortunately find the solution for my case.

Some part of the output for an API connection is as;

stdClass Object (
    [page] => 0
    [items] => 5
    [total] => 5
    [saleItems] => stdClass Object (
        [saleItem] => Array (
            [0] => stdClass Object (
                [reviewState] => approved
                [trackingDate] => 2013-11-04T09:51:13.420+01:00
                [modifiedDate] => 2013-12-03T15:06:39.240+01:00
                [clickDate] => 2013-11-04T09:06:19.403+01:00
                [adspace] => stdClass Object (
                    [_] => xxxxx
                    [id] => 1849681 
                )
                [admedium] => stdClass Object (
                    [_] => Version 3
                    [id] => 721152
                )
                [program] => stdClass Object (
                    [_] => yyyy
                    [id] => 10853
                )
                [clickId] => 1832355435760747520
                [clickInId] => 0
                [amount] => 48.31
                [commission] => 7.25
                [currency] => USD
                [gpps] => stdClass Object (
                    [gpp] => Array (
                        [0] => stdClass Object (
                            [_] => 7-75
                            [id] => z0
                        )
                    )
                )
                [trackingCategory] => stdClass Object (
                    [_] => rers
                    [id] => 68722
                )
                [id] => 86erereress-a9e4-4226-8417-a46b4c9fd5df
            )
        )
    )
)

Some strings do not include gpps property.

What I have done is as follows

foreach($sales->saleItems->saleItem as $sale)
{
    $status     = $sale->reviewState;
    
    if(property_exists($sale, gpps)) 
    {
        $subId      = $sale->gpps->gpp[0]->_;
    }else{
        $subId      = "0-0";
    }
}

What I want is I the gpps property is not included in that string $subId stored as 0-0 in db, otherwise get the data from the string. But it doesn't get the strings without gpps.

Where is my mistake?

Upvotes: 62

Views: 73815

Answers (4)

Matteo Tassinari
Matteo Tassinari

Reputation: 18584

Change

if(property_exists($sale, gpps)) 

with

if(property_exists($sale, "gpps"))

notice how now gpps is passed as string, as per the signature of the property_exists function:

bool property_exists ( mixed $class , string $property )

This function checks if the given property exists in the specified class.

Note: As opposed with isset(), property_exists() returns TRUE even if the property has the value NULL.

Upvotes: 117

Fevly Pallar
Fevly Pallar

Reputation: 3109

Another way , get_object_vars

$obj = new stdClass();
$obj->name = "Nick";
$obj->surname = "Doe";
$obj->age = 20;
$obj->adresse = null;

$ar_properties[]=get_object_vars($obj);

foreach($ar_properties as $ar){
    foreach ($ar as $k=>$v){        
    if($k =="surname"){
        echo "Found";
    }
    }
}

Upvotes: 0

Qullbrune
Qullbrune

Reputation: 1945

Try a simple hack and use count, since the property contains an array, and I guess, that count(array) == 0 is the same case as when the property is not set.

foreach($sales->saleItems->saleItem as $sale)
{
 $status     = $sale->reviewState;

 if(@count($sale->gpps->gpp) && count($sale->gpps->gpp) > 0) 
 {
    $subId      = $sale->gpps->gpp[0]->_;
 }else{
    $subId      = "0-0";
 }
}

Sure, this is not the most beautiful solution, but since the php-function do not work as expected, I thought a bit more pragmatic.

Upvotes: 0

JC.
JC.

Reputation: 650

property_exists is the method designed for this purpose.

bool property_exists ( mixed $class , string $property )

This function checks if the given property exists in the specified class.

Note: As opposed with isset(), property_exists() returns TRUE even if the property has the value NULL.

Upvotes: 2

Related Questions