Connecting Tech Pros Worldwide Forums | Help | Site Map

storing classes

paladin.rithe@gmail.com
Guest
 
Posts: n/a
#1: Jan 24 '06
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.


Carl Vondrick
Guest
 
Posts: n/a
#2: Jan 24 '06

re: storing classes


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.

Justin Koivisto
Guest
 
Posts: n/a
#3: Jan 24 '06

re: storing classes


Carl Vondrick wrote:[color=blue]
> 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 '));[/color]

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 - justin@koivi.com
http://koivi.com
Iván Sánchez Ortega
Guest
 
Posts: n/a
#4: Jan 24 '06

re: storing classes


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

paladin.rithe@gmail.com wrote:
[color=blue]
> Is it possible to store classes in an array? I am fairly new to PHP,
> and haven't found anything either way yet.[/color]

Classes? No. Objects? Definively yes.
[color=blue]
> "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.[/color]

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_eat_s_p_a_m_for_breakfast@hotmail.com
Jabber:ivansanchez@jabber.org ; ivansanchez@kdetalk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD1pX33jcQ2mg3Pc8RAmsNAJwKqNtfSPj0kZ6T0eBFuc daywxohQCfQ/Wk
SAaNzt8xfdIQCzOlG6zK4LA=
=kW+r
-----END PGP SIGNATURE-----
paladin.rithe@gmail.com
Guest
 
Posts: n/a
#5: Jan 24 '06

re: storing classes


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.

Iván Sánchez Ortega
Guest
 
Posts: n/a
#6: Jan 24 '06

re: storing classes


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

paladin.rithe@gmail.com wrote:
[color=blue]
> $array[] = new foo;
> $array[$num]->foofunction();[/color]

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_eat_s_p_a_m_for_breakfast@hotmail.com
Jabber:ivansanchez@jabber.org ; ivansanchez@kdetalk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD1qcS3jcQ2mg3Pc8RAhZ+AJwMUGKh+Q/nJI3zfHJ9qZJ4AJUEdwCfSZld
3fe0spkCWoDHOTFoTVqSQiM=
=xlZG
-----END PGP SIGNATURE-----
Carl Vondrick
Guest
 
Posts: n/a
#7: Jan 25 '06

re: storing classes


Justin Koivisto wrote:[color=blue]
> 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...[/color]


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";}
paladin.rithe@gmail.com
Guest
 
Posts: n/a
#8: Jan 26 '06

re: storing classes


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.

Closed Thread