rossanmol
rossanmol

Reputation: 1653

How to create sub-variables in PHP class

I started using OOP in PHP for first time, and I dont know how to achieve the following structure for my variables:

$this->myData->generalData->title;
$this->myData->generalData->description;
$this->myData->amount;
$this->myData->addNewData();

Up till now, what I am achieving is a normal variable inside a class:

$this->variablename

I tried doing this code, but it's not working at all:

$this->CDNE = "CDNE";
$this->CDNE->FOLDER = "hello man";

Can you explain me, how all this works?

Upvotes: 1

Views: 231

Answers (1)

nanocv
nanocv

Reputation: 2229

Just to ilustrate my comment. Doing it with sub-objects could be something like this (a very basic example without attributes initialization):

class GeneralData{
    public $title;
    public $description;
}

class MyData{
    public $generalData;
    public $amount;

    function __construct(){
        $this->generalData = new GeneralData();
    }

    function addNewData(){
    }
}

class MainClass{
    public $myData;

    function __construct(){
        $this->myData = new MyData();
    }
}

Upvotes: 1

Related Questions