![]() |
|
#1
|
||||
|
||||
How do I access an object from inside another object?Let's assume my page, test.php looks a bit like this:
PHP Code:
So far so good... now if we look in class Orange(), this is what it could look like... PHP Code:
On to Blue()... PHP Code:
I seriously hope that made sense... I can get the code to work flawlessly but I am almost certain there's something in PHP that should let me access an object method/property from within another object or not? __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#2
|
||||
|
||||
|
did you try to make $one a var or made it global in the function?
|
|
#3
|
||||
|
||||
|
I don't understand your question, I didn't make anything global, since I created that object/variable inside test.php.
Even if it were a regular variable, I would usually access it from inside a function this way: $GLOBALS['variable_name']. I was just re-using that logic for this problem and it worked; but I am not comfortable... hence the question. __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#4
|
|||
|
|||
|
I suggest you have a look at references. I'm unfamiliar with the syntax myself, but there's a couple of good giudes around.
Essentially, you've a problem with what you've done above if you create a couple of "Orange"s objects, because with what you've done, the global will be only from the last object you instantiased. Using a reference, you can send the Blue class a reference (like an address inside - not a copy) to the Orange object you want to look into, and then using the reference, Blue willl be allowed to look inside Orange. I'm sorry I've not used them before, so I can't advise syntax, but they're used a lot in C++. But the PHP manual has a nice section on it. GF |
|
#5
|
||||
|
||||
|
So okay... I added a method in Blue() that will accept the other object, passed by reference. So the new class: Blue() looks like this now
PHP Code:
After I edit test.php by adding the one line for the new method in Blue(): PHP Code:
Is this what you mean? FYI, it works... __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#6
|
|||
|
|||
|
Well if it works, I must've meant it
GF |
Recent GIDBlog
Flickr uploads of IA pictures by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Urgernt: Extremely queer Apache access log | moothecow | Apache Web Server Forum | 1 | 20-Dec-2003 02:45 |
| gxx linker accepts only 7 object files | danielxs66 | C++ Forum | 1 | 12-Dec-2003 09:27 |
| FREE 25 MB, No Ads, Control Panel, ASP, ColdFusion, PHP, MySQL, Access Hosting | rkmails | Free Web Hosting | 0 | 08-Sep-2003 05:49 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The