Forum Moderators: coopster

Message Too Old, No Replies

question about OOP

         

ALKateb

1:06 pm on Mar 8, 2011 (gmt 0)

10+ Year Member



Hi everybody, sorry i don't know how to title this topic, and that's why i was not able to search for an answer on google or here in the forums so i thought i should go ahead and post this question.

i have class A and class B, class B will be initiated from within class A and it will be assigned to a property of class A so calling any method in class be will be like this:

$A->B->method();

but while I'm inside class B i might want to access a property from class A! using this will refer to class B itself! what can i do about that? of course using "parent" has nothing to do with this cos B does not inherit class A it was just initiated inside it.

here is an example code:

class classOne{
var $classTwo;
function __construct(){
$this->classTwo = new classTwo;
}
function testOne(){
return "HiOne";
}
}

class classTwo{
function testTwo(){
return ?->testOne;
}
}
$class_one = new classOne;
echo $class_one->classTwo->testTwo();


how can i access testOne from within testTwo? or this is not possible?

well so far all i can do is doing like this
echo $class_one->classTwo->testTwo($class_one);

so if i pass the class object by reference and use it there it would be possible but that's not what i am looking for.

so if there is anyway to achieve this, please let me know.

Thanks

eelixduppy

4:07 am on Mar 9, 2011 (gmt 0)



Don't think this is possible, they are defined in a different scope. Not sure what you are trying to do this for, but there is probably a cleaner implementation than what you are thinking that will allow you to do what you need. Perhaps if you give a more specific example we can help set it straight.

ALKateb

10:15 am on Mar 9, 2011 (gmt 0)

10+ Year Member



eelixduppy

Thanks for your reply, actually what i have is a database class that will be initiated in inside different objects depending on the request and the db object will have to use some of the methods and the properties its creator has and i was looking to find a way to reach the creator methods somehow.

anyway someone told me a way to work around it which is as follows:

class classOne{
var $classTwo;
function __construct(){
$this->classTwo = new classTwo;
$this->classTwo->creator =& $this; // this class assigns itself as creator
}
function testOne(){
return "HiOne";
}
}

class classTwo{
var $creator; // we have a var to hold a reference to the creator

function testTwo(){
return $this->creator->testOne();
}
}
$class_one = new classOne;
echo $class_one->classTwo->testTwo();

so now classTwo has a property creator which references the object that created it.


oh BTW you're an administrator here so i thought maybe i can suggest having bb tags for php codes, even the code tags look weird here, small font with no indenting or whatever. : ) this is a webmasters world i think it should have such thing

thanks for your reply again