Class Definitions inside Objects?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Class Definitions inside Objects?

Post 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?
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post 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
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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, 8O.

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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply