0

I am learning PHP and trying to build my own framework on my own, I will have a front controller approach (index.php) and I was wondering if when I require my files (in my case different classes like Globals.php, BaseController.php) the construct methods of my classe will execute when required.

Index.php

<?php
//1 - Includes global variables
require_once 'config/Globals.php';
 
//2 - Includes Base Controller
require_once 'core/ControladorBase.php';
 
//Front Controller functions
require_once 'core/ControladorFrontal.func.php';
 
//Launch actions through controllers
if(isset($_GET["controller"])){
    $controllerObj=loadController($_GET["controller"]);
    launchAction($controllerObj);
}else{
    $controllerObj=loadController(default_controller);
    launchAction($controllerObj);
}
?>

BaseController

<?php
class BaseController{
 
    public function __construct() {
        require_once 'Connect.php';
        require_once 'BaseEntity.php';
         
        //Include all models
        foreach(glob("model/*.php") as $file){
            require_once $file;
        }
    }
     
    //Plugins and functionalities
     
    public function view($view,$data){
        foreach ($data as $assoc_id => $value) {
            ${$assoc_id}=$value; 
        }
         
        require_once 'core/ViewsHelper.php';
        $helper=new ViewsHelper();
     
        require_once 'view/'.$view.'View.php';
    }
     
    public function redirect($controller=default_controller,$action=default_action){
        header("Location:index.php?controller=".$controller."&action=".$action);
    }
     
    //Methods for the controllers
 
}
?>

3
  • I'd recommend that you go read a little about autoload Commented Mar 8, 2018 at 18:38
  • 1
    No, the constructor is called when instantiated, new BaseController. Commented Mar 8, 2018 at 18:39
  • Wow, you have too many open questions. Time to clean house and accept some answers. Commented Mar 8, 2018 at 19:13

1 Answer 1

1

Nope.

When you run require_once, PHP will simply add this file to your current code, allowing you to access classes/variables inside those files. But the __construct() method only executes when you create a new instance of a class, like $user = new User();.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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