PHP OOP variable scope problem
12-28-2010, 08:10 AM,
#1
PHP OOP variable scope problem
Aight', I've never dug deep into the php OOP thing, as I've always relied on procedural programming or OOP on a very low level...bla...bla...
Silly me, [Image: mr-bean_sq50.jpg?1227175124]
ANYWAY, I've run into a problem, I just can't solve. I donno' whether I should use $this, parent, self, :: (Scope resolution operator). Or whether the static keywords is even supposed to be there.

The case is, that I have a main class, in which I've instantialized(?) two secondary classes (Main -> One and Main -> Two).
Afterwards, through the main class I've changed instance Two's variable $Xenon to be " in ". But I keep getting an:
Code:
Trying to get property of non-object
- error.

Don't mind the resulting sentence, it's just random stuff
Main.php
PHP Code:
class Main
{
    static public 
$One$Two;
    
    function 
Init()
    {
        require(
"child.php");
        require(
"child2.php");
        
        
$this->One = new First;
        
$this->Two = new Second;
        
$this->Two->Xenon " in ";
        
        
$this->One->Activate("the Oven");
    }
}
$Main = new Main;
$Main->Init(); 

child.php
PHP Code:
class First extends Main
{
    function 
Activate($Arg)
    {
        echo(
Second::A().Second::$Xeen.$Two->Xenon.$Arg);
        
//Result i want: Mary's pet was burned in the oven
        //Actual result: Mary's pet was burnedthe oven
    
}


child2.php
PHP Code:
class Second extends Main
{
    static public 
$Xeen " was burned";
    static public 
$Xenon;
    
    function 
A()
    {
        return(
"Mary's Pet");
    }



I've found out, that I can get the result I want if I pinpoint the $Xenon variable from an absolute/global perspective, like this:
PHP Code:
function Activate($Arg)
    {
                      global 
$Main;

        echo(
Second::A().Second::$Xeen.$Main->Two->Xenon.$Arg);
        
    } 
But that kinda sucks, isn't there a relative way to do it? Like where you point to the parent and then point to subject from there? Hope you understand what I mean.

Regards and thanks in advance
- Vanilla


Messages In This Thread
PHP OOP variable scope problem - by Vanilla - 12-28-2010, 08:10 AM
RE: PHP OOP variable scope problem - by RichardGv - 12-28-2010, 04:12 PM
RE: PHP OOP variable scope problem - by Vanilla - 12-29-2010, 12:59 AM
RE: PHP OOP variable scope problem - by RichardGv - 12-29-2010, 11:26 AM
RE: PHP OOP variable scope problem - by Vanilla - 12-30-2010, 02:20 AM
RE: PHP OOP variable scope problem - by Vanilla - 01-03-2011, 09:09 AM

Forum Jump: