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. 7 1545
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($myob ject));
Then later on:
$a = unserialize(fil e_get_contents( 'serialized_cod e.php'));
Good luck.
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($myob ject));
Then later on:
$a = unserialize(fil e_get_contents( 'serialized_cod e.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.co m http://koivi.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 pa***********@g mail.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*********@kde talk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
iD8DBQFD1pX33jc Q2mg3Pc8RAmsNAJ wKqNtfSPj0kZ6T0 eBFucdaywxohQCf Q/Wk
SAaNzt8xfdIQCzO lG6zK4LA=
=kW+r
-----END PGP SIGNATURE-----
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.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 pa***********@g mail.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*********@kde talk.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
iD8DBQFD1qcS3jc Q2mg3Pc8RAhZ+AJ wMUGKh+Q/nJI3zfHJ9qZJ4AJ UEdwCfSZld
3fe0spkCWoDHOTF oTVqSQiM=
=xlZG
-----END PGP SIGNATURE-----
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";}
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alfonso Morra |
last post by:
I have written the following code, to test the concept of storing
objects in a vector. I encounter two run time errors:
1). myClass gets destructed when pushed onto the vector
2). Prog throws a "SEGV" when run (presumably - attempt to delete
deleted memory.
Please take a look and see if you can notice any mistakes I'm making.
Basically, I want to store classes of my objects in a vector. I also
have three further questions:
|
by: Diego F. |
last post by:
Hello. I need to store custom objects in a SQL Server 2000 table. Which is
the easiest way to do it? Do I need to write methods to store each attribute
separately from C# app to the table and the opposite as well?
Regards,
Diego F.
|
by: py |
last post by:
how feasible is it to cache an entire page's content in a session variable?
i am already doing it and it works fine, except i am afraid of scalability
issues. what is the best way to test this? and is there an obvious reason
why i should not do this.
|
by: Jacek Dziedzic |
last post by:
Hello!
Suppose I have a class that contains only public members
of builtin types and a default constructor. Because of the
default constructor it is no longer an aggregate and therefore
no longer POD, according to my understanding of
http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html
I need to be able to serialize this class to/from a binary file
|
by: Mark |
last post by:
I'm consuming a webservice that makes a simple object available. The object
class is marked in the web service as . I have a web
application that consumes and uses this web service's class. When I receive
the object from the web service, I'm interested in storing that object in
ViewState in the web application, but I receive the error below. I'm not
shocked that the web application can't serialize the object as the
attribute isn't...
| |
by: ArmsTom |
last post by:
I was using structures to store information read from a file. That was
working fine for me, but then I read that anything stored in a
structure is added to the stack and not the heap. So, I made a class
that stores the same information.
The user selects any number of records from the file when the program
loads & can then make changes. The records the user selects are added
to an array and changes are made to the records in that array...
|
by: KarlM |
last post by:
After reading some articles regarding confuguration data I'm a bit confused.
Where is the right place for storing configuration data?
- XML-files?
- registry?
- INI-files? (from a users point of view, ini-files are more comfortable to
read and edit)
Where should I store user specific config data?
Where should I store machine specific config data?
|
by: Paul Hadfield |
last post by:
Hi,
I'm not having a lot of luck googling for this one, I want to be able to
store a custom class in the user settings (DotNet2.0, win app). I don't
wish to create public get / set properities for all the things I want to
persist just for storing in the user settings file (because that would allow
other apps to access / change data they shouldn't be able to).
I understand how to browse to the object in VS2005 to add an entry to the...
|
by: John A Grandy |
last post by:
What are some best practices for storing pure dates and pure times in .NET ?
I notice that DateTime.TimeOfDay() returns type TimeSpan , which is
certainly sufficient for storing pure times , but not constrained ...
Do people write their own classes ? Or have I overlooked one or more
intrinsic .NET classes ?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |