Home » Php » oop – PHP include class in other class

oop – PHP include class in other class

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am learning OOP and very confuse to use classes for each other.

I am having total 3 classes

//CMS System class
class cont_output extends cont_stacks
{
    //all methods to render the output
}


//CMS System class
class process
{
    //all process with system and db
}


// My own class to extends the system like plugin
class template_functions
{
    //here I am using all template functions
    //where some of used db query
}

Now I want to use my own class template_functions withing both system classes. But very confused how to use it. Please help me to understand this.

EDIT:
I am sorry champs I forgot to mention that my own class in different PHP file.

How to&Answers:

First, make sure, that you include the class file before using it:

include_once 'path/to/tpl_functions.php';

This should be done either in your index.php or on top of the class which uses tpl_function. Also note the possibility of autoloading classes:

Since PHP5 you have to possibility to autoload classes. This means you register a hook function that is been called everytime when you try to use a class which’s code file hasn’t been included yet. Doing it you won’t need to have include_once statements in every class file. Here comes an example:

index.php or whatever application entry point:

spl_autoload_register('autoloader');

function autoloader($classname) {
    include_once 'path/to/class.files/' . $classname . '.php';
}

From now on you can access the classes without to worry about including the code files anymore. Try it:

$process = new process();

Knowing this, there are several ways how you can use the template_functions class


Just use it:

You can access the class in any part of the code if you create an instance of it:

class process
{
    //all process with system and db

    public function doSomethging() {
        // create instance and use it
        $tplFunctions = new template_functions();
        $tplFunctions->doSomethingElse();
    }
}

Instance members:

Take the process class for example. To make the template_functions available inside the process class, you create an instance member and initialize it somewhere, where you need it, the constructor seems to be a good place:

//CMS System class
class process
{
    //all process with system and db

    // declare instance var
    protected tplFunctions;

    public function __construct() {
        $this->tplFunctions = new template_functions;
    }

    // use the member : 

    public function doSomething() {
        $this->tplFunctions->doSomething();
    }


    public function doSomethingElse() {
        $this->tplFunctions->doSomethingElse();
    }
}

Answer:

You can extend the template_functions class, then you can use all the functions.

class cont_output extends cont_stacks //cont_stacks has to extend template_functions
{
    public function test() {
        $this->render();
    }
}


class process extends template_functions
{ 
    public function test() {
        $this->render();
    }
}


class template_functions
{
    public function render() {
        echo "Works!";
    }
}