Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #11733

Re: accessing nested unknown unserialized objects

Message-ID <11530910.WaAG51LVq5@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-11-05 13:11 +0100
Subject Re: accessing nested unknown unserialized objects
Newsgroups comp.lang.php
References <73a48b65-ce17-461c-92a5-d681547aa4c9@googlegroups.com>

Show all headers | View raw


catseye.chandra@gmail.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@94.75.214.39>

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

accessing nested unknown unserialized objects catseye.chandra@gmail.com - 2013-11-04 12:37 -0800
  Re: accessing nested unknown unserialized objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-11-05 13:11 +0100
  Re: accessing nested unknown unserialized objects Jerry Stuckle <jstucklex@attglobal.net> - 2013-11-05 08:12 -0500

csiph-web