GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Jun-2003, 08:26
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Question

How do I access an object from inside another object?


Let's assume my page, test.php looks a bit like this:

PHP Code:

<?php

// TEST.PHP

$one = new Orange();
$one->DoSomethingInOrange();

// ... some other code

$two = new Blue();
$two->ProcessSomethingInBlue();
?>


So far so good... now if we look in class Orange(), this is what it could look like...
PHP Code:

<?php

// ORANGE.LIB.PHP

class Orange()
{
  function Orange()
  {
    // constructor = nothing
  }

  function DoSomethingInOrange()
  {
    $this->color = 'Orange';
  }

  function ReturnAValueFromOrange()
  {
    return '"I said '.$this->color.'."';
  }
}
?>


On to Blue()...
PHP Code:

<?php
// BLUE.LIB.PHP

class Blue()
{
  function Blue()
  {
    // constructor = nothing
  }

  function ProcessSomethingInBlue()
  {
    // HERE!!! what happens if for whatever
    // reason, I want to echo
    // $one->ReturnAValueFromOrange()???

    // What I have done right now is something like this
    // but, it doesn't seem like it's very correct
    
    // if some checking fails...

    echo $GLOBALS['one']->ReturnAValueFromOrange();
  }
}

?>


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?
  #2  
Old 07-Jun-2003, 08:51
Allowee's Avatar
Allowee Allowee is offline
Regular Member
 
Join Date: May 2003
Location: The Netherlands
Posts: 339
Allowee has a spectacular aura about
did you try to make $one a var or made it global in the function?
__________________
Pastebin
PHP Documentation Site
Allowee's Blog http://allowee.net
  #3  
Old 07-Jun-2003, 08:58
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
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.
  #4  
Old 07-Jun-2003, 13:04
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
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  
Old 08-Jun-2003, 06:48
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
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:

<?php
// BLUE.LIB.PHP

class Blue()
{
  function Blue()
  {
    // constructor = nothing
  }

  function SetOrangeObj( &$obj )
  {
    $this->OrangeObj = $obj;
  }

  function ProcessSomethingInBlue()
  {
    // HERE!!! what happens if for whatever
    // reason, I want to echo
    // $one->ReturnAValueFromOrange()???

    // Previously....
    // echo $GLOBALS['one']->ReturnAValueFromOrange();

    // now the line looks like this
    echo $this->OrangeObj->ReturnAValueFromOrange();
  }
}

?>


After I edit test.php by adding the one line for the new method in Blue():
PHP Code:

<?php
// TEST.PHP

$one = new Orange();
$one->DoSomethingInOrange();

// ... some other code

$two = new Blue();
$two->SetOrangeObj( $one ); // <-- here's the added line....
$two->ProcessSomethingInBlue();

?>


Is this what you mean? FYI, it works...
  #6  
Old 10-Jun-2003, 08:51
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Well if it works, I must've meant it

GF
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 21:36.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.