Klaaz
Klaaz

Reputation: 1640

php class extends, variable from other extended class

I have a main class and two extended classes:

class Main {
    public $foo;
}

class A extends Main {
  public function setVar()
    {
      $foo = "test"; 
    }
}

class B extends Main {
  public function getVar()
    {
      return $this->foo;
    }
}

$A = new A;
$B = new B;

$A->setVar();

echo "result: ".$B->getVar();

But result ($B->getVar()) stays empty. I am clearly missing something simple here... Besides that, is this the way to go splitting long classes up in relevant subclasses?

Upvotes: 1

Views: 8574

Answers (4)

Vlad Balmos
Vlad Balmos

Reputation: 3412

change:

class A extends Main {
    public function setVar()
    {
     $foo = "test"; 
    }
}

to:

class A extends Main {
    public function setVar()
    {
     $this->foo = "test"; 
    }
}

Notice the $this keyword

Upvotes: 4

Cranio
Cranio

Reputation: 9848

To further clarify things, it may help to think it in this way. Let's say your Main is a class that stands for a vehicle (and hence we rename it Vehicle), so class means that Vehicle defines the characteristics of a veicle but not a particular one to do operations with (we need an instance for that). Let's translate also your class A in Car and B in Truck: therefore they are specializations of your main class that, along with possessing every characteristic of a generic Vehicle, extend the concept of "vehicle" and point out specific behaviours and properties of Cars in general, and Trucks in general, but - and that's the point - don't reference a particular car or truck. We need instances for that.

class Vehicle
{
   public $numberOfTires;
}

class Car extends Vehicle
{
    function smashMe()
    {
        echo "Oops!";
    }
}

class Truck extends Vehicle
{
    function smashMe()
    {
        echo "More Oops!";
    }
}

From this point we can define particular instances (your Porsche, my Camaro... different cars) on which we may perform operations (calling methods) and set properties.

$genericVehicle = new Vehicle;
$myPorsche = new Car;
$yourCamaro = new Car;
$hisTruck = new Truck;
$herTruck = new Truck;

But every instance remains independent from the other.

Upvotes: 2

Mark Baker
Mark Baker

Reputation: 212522

Example of using Dependency Injection (DI) to share an instance of Main between to different class instances rather than trying to use inheritence

class Main {
    public $foo;
}

class A {

    protected $main;

  public function setVar($data)
    {
      $this->main->foo = $data;
    }

  public function __construct(Main $main)
    {
      $this->main = $main;
    }
}

class B extends Main {
    protected $main;

  public function getVar()
    {
      return $this->main->foo;
    }

  public function __construct(Main $main)
    {
      $this->main = $main;
    }
}

$M = new Main;
$A = new A($M);
$B = new B($M);

$A->setVar("test");

echo "result: ".$B->getVar();

Upvotes: 2

Madara's Ghost
Madara's Ghost

Reputation: 175088

Both are different instances of the the same class.

Operations done on one instance, does not apply on any of the others (That's kinda the point of OOP).

To do what you want, You can have B extend A, and then perform both operations on a single instance of the B class.

Upvotes: 1

Related Questions