See the two versions:
Version A:
<?php
function mainfunc() {
global $obj1;
global $obj2;
global $obj3;
$obj1->interact();
$obj2->interact();
$obj3->interact();
}
$obj1 = new Class1();
$obj2 = new Class2();
$obj3 = new Class3();
mainfunc();
?>
Version B:
<?php
class MainClass() {
var $obj1;
var $obj2;
var $obj3;
function MainClass() {
$this->obj1 = new Class1();
$this->obj2 = new Class2();
$this->obj3 = new Class3();
}
function mainfunc() {
$this->obj1->interact();
$this->obj2->interact();
$this->obj3->interact();
}
}
$mainobj = new MainClass();
$mainobj->mainfunc();
?>
In words:
In version A the main flow is managed in a procedural way: a main function (mainfunc) takes the control on the definition of the objects and the interaction with those.
All the objects are separately instantiated, and are global.
Running the code is calling the main function.
In version B all the code is written with an oo approach. From the beginning, a main object is instantiated (mainobj), and the main function (mainfunc) is a method of that object.
All the objects are 'associated' to the main object, so those are 'contextually' instantiated with the main object.
Running the code is invoking the main object.
What do you prefer?
I'm interested in comments about the memory usage in the two scenarios.