Another PHP question.

  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post January 27th, 2009, 10:50 pm

I sometimes come across pieces of code that look like $obj->something->variable... how is that possible? I would want to understand how that is done...
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post January 27th, 2009, 10:50 pm

  • IcyDragoon
  • Student
  • Student
  • No Avatar
  • Joined: Mar 12, 2008
  • Posts: 65
  • Status: Offline

Post January 27th, 2009, 11:25 pm

I guess yes since it is an OOP language.

for example:

class->arrayofclasses[1]->variable

i guess you can called it obj within obj; subclass..

http://www.hudzilla.org/phpbook/read.php/6_6_0

just like a regualr class that embeded another class.

Cat->Fur->Color;
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6130
  • Loc: Seattle, WA
  • Status: Offline

Post January 28th, 2009, 8:07 am

Code: [ Select ]
<?php

class MyFirstClass {

public $aVariableFromFirst = "hello";
}

class MySecondClass {

public $aVariableFromSecond = new MyFirstClass();
}

?>
  1. <?php
  2. class MyFirstClass {
  3. public $aVariableFromFirst = "hello";
  4. }
  5. class MySecondClass {
  6. public $aVariableFromSecond = new MyFirstClass();
  7. }
  8. ?>


Code: [ Select ]
<?php

$obj = new MySecondClass();
echo $obj->aVariableFromSecond->aVariableFromFirst;

?>
  1. <?php
  2. $obj = new MySecondClass();
  3. echo $obj->aVariableFromSecond->aVariableFromFirst;
  4. ?>
The Beer Monocle. Classy.
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post January 28th, 2009, 9:20 am

Ahh... this should fix a huge problem that I have with some of my classes. Thanks so much Spork and IcyDragoon.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 107 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.