PHP references to $this

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

PHP references to $this

Post by yacahuma »

I want to be able to call the main class from an object of another class. Take a look
What am I missing. Thank you

Code: Select all

 
<?php
class SatClass
{
     var $J;
     var $G;
     var $X;
 
     var $parent;
 
     function __constructor($ref)
     {
         $this->parent = $ref;
     }
 
     function calc()
     {
         return 5 * 5;
     }
 
     function calc5()
     {
         return  $this->parent->getA() * 5;
     }
}
 
class Main
{
    var $A;
    var $B;
    var $C;
 
    var $S1;
 
    function __construct()
    {
        echo "Calling Constructor";
        $this->S1= new SatClass($this);
    }
 
    function getA()
    {
      return $this->A;
    }
}
 
$main = new Main();
$main->A = 5;
echo "DATA=" . $main->getA();
echo "DATA=" . $main->S1->calc();
echo "DATA=" . $main->S1->calc5(); //ERROR Call to a member function getA() on a non-object in 
?>
 
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP references to $this

Post by VladSun »

It's a Pascal error ;)

function __constructor !== function __construct()
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: PHP references to $this

Post by yacahuma »

Good Lord, I did not see that. Can php just create and alias???
Post Reply