468,256 Members | 1,426 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,256 developers. It's quick & easy.

storing classes

Is it possible to store classes in an array? I am fairly new to PHP,
and haven't found anything either way yet.
I have a program that where you can have multiple notes attached to a
ticket, which are stored in a database. I would like to just pull all
the notes from the database, storing each one in a seperate class,
which itself is stored in an array (well, another class, but it's a
classList, it's mainly an array).
I'm getting an error when I run the page:
"Cannot use a scalar value as an array", and "Call to a member function
on a non-object". The second is related to the first, as I'm calling a
function on the array value.
If it is possible, I probably coded something wrong. If that's the
case, I'll post some code.
Thanks for the help.

Jan 24 '06 #1
7 1293
You can "store" objects in an array, sure:

$my_array = array(new my_object(), new_my_object());

That should work just fine (untested).

You can also try and serialize objects, which are useful if you want to
save them after execution is over. To do this, you use serialize()and
unserialize(). For example:

fwrite($handle, serialize($myobject));

Then later on:

$a = unserialize(file_get_contents('serialized_code.php '));

Good luck.

Jan 24 '06 #2
Carl Vondrick wrote:
You can "store" objects in an array, sure:

$my_array = array(new my_object(), new_my_object());

That should work just fine (untested).

You can also try and serialize objects, which are useful if you want to
save them after execution is over. To do this, you use serialize()and
unserialize(). For example:

fwrite($handle, serialize($myobject));

Then later on:

$a = unserialize(file_get_contents('serialized_code.php '));


Be sure that you have included the class definitions *before* you
attempt to unserialize an object, or you may have some problems.

I haven't tried serializing objects in php, but I was just thinking that
in php5 because of the pass by reference default nature, you may have
difficulty with it if you aren't using the clone keyword...

--
Justin Koivisto, ZCE - ju****@koivi.com
http://koivi.com
Jan 24 '06 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pa***********@gmail.com wrote:
Is it possible to store classes in an array? I am fairly new to PHP,
and haven't found anything either way yet.
Classes? No. Objects? Definively yes.
"Cannot use a scalar value as an array", and "Call to a member function
on a non-object". The second is related to the first, as I'm calling a
function on the array value.


My guess is that you are doing something like:

<?php

$object = new foo;
$array[$object] = $counter;

?>

When you should be doing:

<?php

$object = new foo;
$array[$counter] = $object;

?>

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
MSN:i_*************************@hotmail.com
Jabber:iv*********@jabber.org ; iv*********@kdetalk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD1pX33jcQ2mg3Pc8RAmsNAJwKqNtfSPj0kZ6T0eBFuc daywxohQCfQ/Wk
SAaNzt8xfdIQCzOlG6zK4LA=
=kW+r
-----END PGP SIGNATURE-----
Jan 24 '06 #4
I think I see the issue. What I'm doing is this:

<?php

$array[] = new foo;
$array[$num]->foofunction();

?>

I'll bet it doesn't like that. I'll try doing it your way first to see
what happens.

Jan 24 '06 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

pa***********@gmail.com wrote:
$array[] = new foo;
$array[$num]->foofunction();


Are you sure that $array[$num] contains an instance of foo?? What is $num???

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
MSN:i_*************************@hotmail.com
Jabber:iv*********@jabber.org ; iv*********@kdetalk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD1qcS3jcQ2mg3Pc8RAhZ+AJwMUGKh+Q/nJI3zfHJ9qZJ4AJUEdwCfSZld
3fe0spkCWoDHOTFoTVqSQiM=
=xlZG
-----END PGP SIGNATURE-----
Jan 24 '06 #6
Justin Koivisto wrote:
I haven't tried serializing objects in php, but I was just thinking that
in php5 because of the pass by reference default nature, you may have
difficulty with it if you aren't using the clone keyword...

I just tried it out on my machine: it works as expected.

<?php
class MyObject
{
public $name;

public function __construct()
{
$this->name = 'Carl';
}
}

$a = new MyObject;
$b = & $a;

print serialize($b) . "\n" . serialize($a);
?>

Returns:
O:8:"MyObject":1:{s:4:"name";s:4:"Carl";}
O:8:"MyObject":1:{s:4:"name";s:4:"Carl";}
Jan 25 '06 #7
I'm doing the work in a loop, and $num would be the location of the
object: like $array[1]->foofunction();
I changed my code to the other way though, and it seems to be working
correctly. Now it's just not pulling information, or pulling the wrong
information, I'm not sure yet. But that's something else entirely.
Thanks for the help everyone.

Jan 26 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Diego F. | last post: by
2 posts views Thread by Jacek Dziedzic | last post: by
3 posts views Thread by ArmsTom | last post: by
2 posts views Thread by Paul Hadfield | last post: by
4 posts views Thread by John A Grandy | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.