Sevengames Xoom Sevengames Xoom - 4 months ago 20
PHP Question

How to call the index function inside a class when a file is load?

I'm wondering how I can call the index function inside the class

App
, an example:

the user put this URL in the browser:
www.application/controllers/application.php


the file
application.php
contain this:

<?php

Class App{

public function index()
{
// do stuff..
}
...


I want that when this file is opened in the browser, the index function must be executed automatically... How can I achieve that?

Answer

You can call index on an instance of App.

$app = new App();
$app->index();

However, it feels like there is something missing here. Typically you would have a router that would handle instantiating the correct controller based on the route requested by the user.

You can use the above code to manually instantiate the app controller and call index, but again, I think there is a missing piece.