Various functions that will do the same thing

Asked

Viewed 47 times

0

I have the following function

function about_about(){
    $this->render();
}

However, I have another 100 pages, which will only have the render(), how can I do a function just to save code and time? Where I set the pages that will be displayed and give the render(), remembering that about_about, in this example, is the name of the page that comes by url, in this case, the name of the method.

  • Do you want to call the same method by different URL? Like creating a generic controller?

  • Man, I don’t know about PHP.. but there is no possibility of you creating a class, which has this method that is common to all and make all other classes inherit the first no?

  • That’s right Alan, I want to call several methods with the same ending... because they will all render, e.g.: Function x(){ render(); } Function y(){ render(); }...

1 answer

2


Yes, using the concept of Inheritance of Objects, which is part of the object orientation.

To create class inheritance you can do so:

Create a file, for example: comum.php, with the following content:

<?php
class base  extends CI_Controller {
    function render() {
        echo 'Comum a todas as classes.';
    }
}

In your controller, you will use:

<?php
include 'comum.php'; // incluir o arquivo da classe base
$about = new base();
$about->render();

The result will be the output of the method that has been extended from the base class:

Comum a todas as classes.

I hope to have helped and wish you good luck in your project!

  • What I wanted to avoid, is to have to repeat several times the same code, changing only the name of the function... so, but I think there is no other way right?

  • 1

    I edited the answer, if I could understand you correctly, I think now you have what you’re looking for there.

  • I’m using the Codeigniter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.