I just started wondering.. I have a bass class, let's say a class called Framework. This class contains a lot of functions and sets a couple of variables that are used inside other classes. What would be the most efficient way to access these variables from inside other classes? Should I have them extend the Framework class, or should make the variables available in some other way?

Side question: if I have 5 classes that all extend the Framework class, does that mean that the Framework class is instantiated and constructed 5 times during the execution of the script?

Member Avatar for diafol

Make it an abstract class?

abstract class Base{
    ...
    public function __construct(){
        ...
    }

    protected function sumThem($num1, $num2){
        return intval($num1) + intval($num2);
    }

}

class SubClass extends Base{
    ...
    public function __construct(){
        parent::__construct();
        ...
    }
}


$sc = new SubClass();

echo $sc->sumThem(3,6);

Is that what you're looking for? I don't know which alternative you could use. Maybe a singleton pattern? So your classes (not subclasses of Base now), could access like this:

public function __construct(){
    if (class_exists('stnClass')) {
        $this->stn_class = stnClass::getInstance();
    }
}

There's only ever one instance of this stnClass. This approach is frowned upon by many though.

I suppose you could even pass the Base object as a parameter:

$base = new Base();
$sc = new SubClass($base); 

But, I may be wrong, that's not how you do inheritence. The SubClass isn't extending Base.

Well that does add something to my knowledge, thanks :)! After a few moments of thinking, I decided I'll just make all my classes globally available so that I can use them wherever I want. There's such a close cooperation between all the classes, I couldn't think of a better way.

Member Avatar for diafol

What does that mean? Base class with subclassing that (inheritence)?

You mean the required cooperation? That means that I have over 5 classes that all make use of each other's functions. So what I do now is I global the var name of the required class in a class's constructor and save it to $this->globalclassname to make it available through the whole class. Probably not the cleaneast solution, but it does work and it works fine for now :).

Member Avatar for diafol

Probably not the cleaneast solution

Making things global is rarely good, but if you're happy...

Well it doesn't hurt does it? :)

I mean I don't want to decrease performance by reconstructing each class each time one class is created. Hmm dilemma..

Member Avatar for diafol

How about using static methods then? For example:

class staticFunctions{

    public static function sayHello(){
        echo "hello";   
    }

}

class concreteClass{

    public function sayHello(){
        staticFunctions::sayHello();    
    }

}

$rubbish = new concreteClass();
$rubbish->sayHello();

This will output 'hello'.

Again, I think their use is broadly discouraged. Static methods are often shoved into a class to avoid collisions. You don't have to create an instance of this class either (staticFunctions in the example).

Minitauros you said that you have a class that contains a lot of functions and some variables that are used inside other classes in the same flow.

If this class has really a lot of functions –methods , then probably there is something wrong with it. Reading a class you must understand a small story and reading its function you recognize paragraphs of it. More over there is a special meaning of inheritance of classes that are objects understanding by their abstract parent to which book are into.

First of all I would recommend separate the functions of this monster super class by their mean. Then understand for each group of functions if corresponds to an object class or to a utilities class.

Utilities classes are classes with static methods (as diafol said) for the same type (e.g. a class _String could have a static method firstPart that would get a String and a delimiter and return the String till the delimiter if there is one)

Of course it is really easy to write OOP as functional with Utilities classes if you don’t recognize what methods are part of the object (as behaviours of it) and what are just utilities of it with no alternation to the short story that the object class tell.

In your first post you referred to an example “Framework” class. Well if you have a FrontController or just a Controller in an MVC architecture then the classes “used” by it, can have as constructor parameter the property that is needed. For example if a Controller instantiate a DB object then of course you should pass it in the Model class that will use it. There is a chance to pass the same the Controller instance (as this) in really few cases, for example if you have a family of operations in their abstract class.

Hope I helped a bit …

That does all help, yea :)! Thanks! I have just one last question. I know I can use, as diafol says, a class inside a class by referring to its full name. E.g.

class: Database
function: __construct
content of function: $this->settings = Framework::getSettings('database');

The question is: can I always refer to Framework, even if I have not instantiated it within the function itself, or is there something about the use of this that I must absolutely know?

In the example you gave you don’t instantiate “Framework” class at all. Instantiating an object means that we create a new instance of it (in PHP with new Something() ) . What you are doing is calling the static method “getSettings” of your “Famework” class.

From your example I can’t see the reason of not passing what ever the Framework::getSettings('database') returns to the constructor of the “Database” class. That way you separate the logic of the “Database” from the “Framework” class, more over you can make the use of it more generic (without needing the “Framework” class) . If you use that class from within the “Framework” class there is no need that method “getSettings” to be static , could be even private or protected (if it is in the abstract parent) instantiate the “Database” class with $db = new Database($this-> getSettings('database'));

The constructor function of the “Database class should be then
__construct($dbSettings)
Of course you may need that variable as private in the “Database” class so make a private $dbSettings and in the __construct method
$this->dbSettings = $dbSettings;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.