8

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.

3
  • I am confused about what is your confusion... Commented Apr 23, 2013 at 16:58
  • sounds like you want multiple inheritance, so that cont_output extends BOTH cont_stacks AND template_functions? because otherwise, there's no reason class #1 can't call a static method, or instantiate an copy of class #2 within itself. Commented Apr 23, 2013 at 17:05
  • @MarcB It's kind of but depends only thing is how I can use my own class available in both classes. Commented Apr 23, 2013 at 17:10

2 Answers 2

15

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();
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for quick response.. just few more queries.. protected is a variable so does it required $ ? and I forgot to mentioned my class in separate php file
So where to include my file?
I'll give an example for autoloading.. Give me a couple of minutes. You should know about this...
Ok fine. You should read about autoloading anyway, as it is a cool feature of php! :)
Sure! I will do that.. I really need to understand all these.. thanks a lot
1

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!";
    }
}

1 Comment

But than what about class process? sorry but trying to understand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.