Re: accessing nested unknown unserialized objects [message #183640 is a reply to message #183638] |
Tue, 05 November 2013 12:11 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma:
|
Senior Member |
|
|
catseye(dot)chandra(at)gmail(dot)com wrote:
^^^^^^^^^^^^^^^^^^^^^^^^^
Please get a name, preferably a real one.
> I'm currently fiddling with phpgallery 1.x import/tweaking, and by using
> unserialize got access to an album metadata.
>
> unserialize gives a big array, with nested objects as each elt.
>
> I cannot find the right way to wright the accessor to two fields of real
> interest :
>
> $this->image->name
>
> and
>
> $this->caption
Apparently you are not within a dynamically called method, so $this has no
meaning.
> for now I got :
>
> <?
Should be
<?php
see also <http://pear.php.net/manual/en/standards.tags.php>.
> $fh = fopen("photos.dat", "r");
> $buf = fread($fh, 1024000);
This may get you an incomplete serialization in $buf. In order to read a
file of unknown size into a string without sending it to the standard
output, use file_get_contents() instead (and handle potential errors).
(1024000 bytes are 1024 kB, not 1024 KiB = 1 MiB.)
You also forgot to fclose() after that. Although not strictly necessary
here (that is, *iff* the program completes, the file is closed
automatically), it is a good idea to free resources as soon as possible.
<http://php.net/file_get_contents>
<http://php.net/fclose>
> $arr = unserialize($buf);
> foreach ($arr as &$o) {
> print $o->image->name;
> print $o->caption;
This will output the values without delimiter, which is probably not what
you want.
> flush();
This slows down the output and should be unnecessary unless you want output
in real-time.
<http://php.net/flush>
> }
> ?>
This line should be removed if nothing follows, in order to prevent unwanted
output.
<http://php.net/manual/en/language.basic-syntax.phptags.php>
> also:
>
> […]
> printf("%s : %s\n", $o->image->name, $o->caption);
Much better.
> […]
>
> but nothing get printed :-(
Insert
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
as first statements in your program, and run it again. Since you should not
do experimental work on a remote production system, these settings should be
in your (local) php.ini.
<http://php.net/error_reporting>
<http://php.net/ini_set>
> […]
> --
Be careful not to use lines containing only “-- ” as separators between
prose and code/output in a posting; they are reserved for signature
delimiters. (Fortunately, you or Google Groups – which otherwise is very
buggy – have omitted the space here, so what followed was not truncated by
my newsreader on following up.)
> Array
> (
> [0] => __PHP_Incomplete_Class Object
> (
> [__PHP_Incomplete_Class_Name] => AlbumItem
> [image] => __PHP_Incomplete_Class Object
> (
What *exactly* is this the output of?
PointedEars
--
Sometimes, what you learn is wrong. If those wrong ideas are close to the
root of the knowledge tree you build on a particular subject, pruning the
bad branches can sometimes cause the whole tree to collapse.
-- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom(at)94(dot)75(dot)214(dot)39>
|
|
|