0

I am new to php specially OOP. I need to access nested object elements. MyNewJamaContourServiceGet is the class whose object is $jama Print_r($jama)gives this object structure :

MyNewJamaContourServiceGet Object
(
    [result:JamaContourWsdlClass:private] => JamaContourStructGetItemsFromTextSearchResponse Object
        (
            [return] => JamaContourStructGetItemsFromTextSearchResponse Object
                (
                    [return] => Array
                        (
                            [0] => JamaContourStructWsItem Object
                                (
                                    [childDocumentTypeId] =>
                                    [createdDate] => 2014-03-08
                                    [currentUserWritePermissions] =>

I tried accessing it like :

 var_dump($jama->result->JamaContourWsdlClass);
   var_dump($jama['result:JamaContourWsdlClass']['return']['return'][0]['JamaContourStructWsItem']['createdDate']);

It gives NULL. What i'm doing wrong. I also tried converting it to array with type casting and json_encode(json_decode($jama)); but no luck.

MODIFIED : As suggested by the Dev.Bushido I modified $result to public now i get following object :

MyNewJamaContourServiceGet Object
(
    [result] => JamaContourStructGetItemsFromTextSearchResponse Object
        (
            [return] => JamaContourStructGetItemsFromTextSearchResponse Object
                (
                    [return] => Array
                        (
                            [0] => JamaContourStructWsItem Object
                                (
                                    [childDocumentTypeId] =>
                                    [createdDate] => 2014-03-08T01:21:18.867Z
                                    [currentUserWritePermissions] =>

I tried accessing it with following :

var_dump($jama['result']['return']['return'][0]['createdDate']);

 var_dump($jama->{'result'}->{'return'}['return'][0]['JamaContourStructWsItem']['createdDate']);

Why i am still not able to access them as they are not private anymore?

Thanks a lot Dev.Bushido! :) Here is the function which i added in the class to search for key in object to retrieve it's value :

    class MyNewJamaContourServiceGet extends JamaContourServiceGet{



            public function findValue($jama, $key)
            {
    #           if(array_key_exists($key, $jamaContourServiceGet))
    #           return true;
                    echo "before foreach";
                    foreach($jama as $key1 => $value1)
                    {
                            echo "Inside foreach Key1 value is : ". $key1;
                            echo "Vlaue1 is : ". $value1;
                            if (in_array($key, $value1))
                            {
                                    echo "return value1 is : ". $value1;
                                    return $value1;
                            }
                            elseif(is_array($value1))
                            {
                                    echo "Inside elseif value1 is an ARRAY : ". $value1;
                                    return findValue($value1, $key);
                            }
                    }

             return false;
            }
}

$key ="label";
$r1=$jama->findValue($jama,$key);

if($r1)
{
    echo "The element is in the array" . "\n";
    echo $key ." : " . $r1 . "\n";
} else  {
            echo "Key not found ";
            echo $r1;
        }

Is this function right ? as only echo before foreach get executed and i get key not found.

I updated my function as you said :

class MyNewJamaContourServiceGet extends JamaContourServiceGet{

  private $test;
        public function Add($add){
                $this->test=$add;
        }

        public function FindValue1($object, $key)
        {
                echo "Inside FindValue1 \n";
                echo "before if \n ";
            if (is_null($object->{$key})){
                echo "It is null \n";
                    return false;
                } else if (is_object($object->{$key})){
                        echo "inside else if \n";
                        return FindValue1($object->{$key}, $key);
                  }
                    return $object->{$key};
        }
}

$test = new MyNewJamaContourServiceGet($wsdl);
$testTest = new MyNewJamaContourServiceGet($wsdl);

$testTest->Add(0);
$test->Add($testTest);

var_dump($test->findValue1($test,'label'));

I get the result as :

Inside FindValue1 before if

Notice: Undefined property: MyNewJamaContourServiceGet::$label in /opt/lampp/htdocs/JAMA/jamaContour/test_import.php on line 107
It is null
bool(false)

Please Help!

1 Answer 1

2

properties of an object are accessed differently then you're doing right now.

try:

var_dump(var_dump($jama->result->return->return[0]->createdDate);

Is this the function you want:

public function FindValue($object, $key)
{
    if (is_null($object->{$key})){
        return false;
    } else if (is_object($object->{$key})){
        return $this->FindValue($object->{$key}, $key);
    }

    return $object->{$key};
}
Sign up to request clarification or add additional context in comments.

6 Comments

@user3520135 can you add the code of your class if this doesn't work, because the problem might lie somewhere else.
It's is working but i think i should add a function in the main class to get value just like you said. As making $result public is not good.
If you want to add a function that returns the value of the result, then you should add it in the MyNewJamaContourServiceGet class, otherwise you would have the same problem again ;)
Actually what i am actually doing is trying to search for key in this object and return that key's value. so i added a function of findvalue in MyNewJamaContourServiceGet class but it was not getting executed.I will try it now i think it's shouldn't have any problem now. I will add the function code in the question.
Thanks ! but Nope this doesn't work i think because of Array's inside objects. and yes i want to search object with key parameter and return value of the key.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.