Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP include class in other class

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.

like image 583
Code Lover Avatar asked Apr 23 '13 16:04

Code Lover


People also ask

How can use class from another class in PHP?

In PHP, classes are declared with the class command while objects are declared with the new keyword. Inheritance is initialized using the extends keyword. Inheritance is a concept in PHP OOP, where classes are created out of another class. Simply put, it means to derive one class from another.

HOW include class from another file in PHP?

Use include("class. classname. php");

How do you call a class in PHP?

PHP: Class Constants When calling a class constant using the $classname :: constant syntax, the classname can actually be a variable. As of PHP 5.3, you can access a static class constant using a variable reference (Example: className :: $varConstant).

What is the use of use keyword in PHP?

The use keyword has two purposes: it tells a class to inherit a trait and it gives an alias to a namespace.


2 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();
    }
}
like image 142
hek2mgl Avatar answered Oct 05 '22 15:10

hek2mgl


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!";
    }
}
like image 37
Tim Avatar answered Oct 05 '22 17:10

Tim