Forum Moderators: coopster

Message Too Old, No Replies

Classes Within Classes Scope Question

How do you access a variable in a parent class from within the child class?

         

Sekka

2:35 pm on Nov 6, 2006 (gmt 0)

10+ Year Member



Hi,

Simple enough question I can't seem to find the answer to.

I have two classes, A and B.

B is created as an object within A, but also needs to use a variable within A. How do you reference it?

Example,

class A {
var somevar = "test";
A { $obj = new B (); }
}

class B {
B { print ( * $somevar from the A class * ); }
}

The class B is trying to use the variable $somevar declared in class A. I just don't know how to use the variable.

Thank you.

N.B. I am running PHP 4 not 5

dreamcatcher

2:58 pm on Nov 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Sekka,

Have you tried extending the class?

class B extends A

Then just reference is using:

$this->$somevar;

dc

Sekka

3:21 pm on Nov 6, 2006 (gmt 0)

10+ Year Member



I have worked on another forum that I can't do what I am wanting to do.

Class A changes the variable's value, and the minute class B is declared it is reset to it's default value.

Thanks anyway.