0

I have some code:

Test Controller:

        Class test extends CI_Controller{
            public function print_object(){
                 $x = (object) array('a'=>'A', 'b'=>'B', 'C');
                 echo '<pre>'.print_r($x, true).'</pre>';
            }
        }

Test2 Controller:

        Class test2 extends CI_Controller{
            public function get_printed_object(){
                 $url = "http://localhost/project/test/print_object";
                 (object) $str = file_get_contents($url);
                 echo $str->a; //won't make it. resulting error
            }
        }

the line

echo $str->a;

was resulted a warning : Trying to get property of non-object

Is it possible for me to re-make the $x object that was printed to string?

2 Answers 2

1

The main issue that you have is that file_get_contents returns a string with the output of the url. $str is therefore a string only and even the cast won't change that.

If you want to convert it to an object then you can json_encode (or serialize) and output that in test. test2 would then have to json_decode($str) to recreate the object.

Sign up to request clarification or add additional context in comments.

Comments

0

You should use the native serialize() and unserialize() functions. by serializing an object you will obtain a json-like (its not json, but looks like) string with all the information. When you unserialize, you obtain a clone of the serialized object, and can access its methods and properties in a normal way.

check out the php manual for this functions:

http://php.net/manual/en/function.unserialize.php

http://php.net/manual/en/function.serialize.php

and this last page for a cool example:

http://php.net/manual/en/language.oop5.serialization.php

Comments

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.