Page 1 of 1
Class Definitions inside Objects?
Posted: Sun Mar 13, 2005 3:40 pm
by Ambush Commander
Two questions:
1. Are you allowed to define a class inside another class, like this:
Code: Select all
class Author {
class Story {
//More Information
}
//Use Class Here
}
and
2. If it is allowed, should you do it? Or will this work fine (scope and stuff):
Code: Select all
class Story {
//Define
}
class Author {
//Define and use class Story
}
And an extra question:
3. What is the overhead on serialize? What does it work best on and what does it take a long time to do? How often should you use serialize?
Posted: Sun Mar 13, 2005 3:44 pm
by Sphen001
Hi,
I'm not completely sure, but shouldn't you have the base class Author, and then use the Story class extending that.
It's just a guess. I'm not terribley up on these things.
Sphen001
Posted: Sun Mar 13, 2005 3:46 pm
by Ambush Commander
Well, I actually thought about extending the class, but then I realized that I want to have a bunch of author objects lying around, and then the ability to access objects INSIDE that authorobject.
Code: Select all
$s = $authors['authorname']->storyobjects->storyname;
Come to think of it, is nesting objects allowed?
Posted: Sun Mar 13, 2005 3:47 pm
by feyd
try it.
What are you trying/wanting to do?
edit: well, since you want to use story objects inside others, they should be defined seperately.
Posted: Sun Mar 13, 2005 3:56 pm
by Ambush Commander
Well, in the past, I've had information about an author and his stories stored in three variables, $authinfo, $sc, and $sinf. Thus, each array doesn't actually contain contain a story and it's corresponding values (you'd think that would be the most logical way to do it).
I had been toying with the idea of porting all the data to something like $storyarray['storyname']['storyattribute'] but I learned about objects! They sounded really cool, and I thought that when I was creating a story array, I had to make a call to the database and get the appropriate elements and stuff. __initialize would work perfect for this! Plus, by blueprinting a class, I would be able to explicitely say what variables were allowed, and which ones were not, while having the extendability (for like, Chapter information) that Arrays offer. It would help solve a lot of my "Undefined Index" woes.
But "Try it" that's a very good idea. I should have thought of that,

.
Code: Select all
$authorarray[$authorobject->current]->{$storyobject->current}->title;
//I hope grouping is supported... time to start testing!
Hmm... now we just have to figure out serialize(). Serialize is fast, right?
EDIT: After seeing Feyd's second post, my question is they are to be defined seperately, but should they be defined within the author class if they're only going to be used there? And Class definitions are global, correct?
Posted: Sun Mar 13, 2005 4:10 pm
by feyd
class definitions are global once the code that defines them is run.
You can use references to do your example

Code: Select all
$authors->currentAuthor->currentStory->title;
$authors would be an iterator pattern, I believe.
As for serialize, it's decently fast.
Posted: Sun Mar 13, 2005 4:15 pm
by Ambush Commander
Well, the answers most of my questions: Since The story class is almost definitely going to be run at some time, no point in hiding it away I guess.
I'll probably start using Serialize for caching information. Is serialize faster than explode (because if an array is numbered, then is it faster to store it using some cryptic seperating string "like (@#$8adfe0)" that's unlikely to be used. Hmm... but serialize would prevent problems like someone actually stumbling upon the seperating string...
Posted: Sun Mar 13, 2005 4:25 pm
by feyd
serialize is built specifically for this. It performs well. Just remember that in order to unserialize() you must have the class definition already available.