Jump to content

Requesting a little help in my bootstrap code


Go to solution Solved by trq,

Recommended Posts

Hello,

 

I would like to request a little of help in my code, I m trying to do an database for my movies in php, instead of using an framework or just do code without any structure, i decided to try adventure my self into higher grounds and try to make the application as a little framework that i could use for future projects the main reason behind this is mainly to learn proper PHP programing skills. In the past few weeks i have read online and changed my code so many times but or i m not understanding the class programing style or i m missing something. Here's my Work so far:

 

Index.php

<?php
    defined( 'DS' )         or define( 'DS', DIRECTORY_SEPARATOR );
    defined( 'ABSPATH' )    or define( 'ABSPATH', dirname(__DIR__) );
    defined( 'SYSPATH' )    or define( 'SYSPATH', ABSPATH . DS . 'System' );
    defined( 'WEBPATH' )    or define( 'WEBPATH', ABSPATH . DS . 'Public' );
    defined( 'PKGPATH' )    or define( 'PKGPATH', ABSPATH . DS . 'Packages' );
    defined( 'APPPATH' )    or define( 'APPPATH', PKGPATH . DS . 'Application');
    defined( 'COREPATH' )   or define( 'COREPATH', PKGPATH . DS . 'Framework' );

    require ( PKGPATH . DS . 'Bootstrap.php');

Bootstrap.php

<?php
    class Bootstrap{
        
        static protected $_appConfig;

        static public function init ( ){
            self::$_appConfig = APPPATH . DS . 'Config' . DS . 'Main.php';
            require self::$_appConfig;
            var_dump($appOptions);
        }
        
        static private function setEnv( $appOptions ){
            /** Set application enviroment to developer or 'production' */
            Switch( is_bool( $appOptions['Developer'] ) ? $appOptions['Developer'] : FALSE ){
                case TRUE:
                    error_reporting( E_ALL );
                    ini_set( 'display_errors','On' );
                    break;
                default:
                    error_reporting( E_ALL );
                    ini_set( 'display_errors', 'Off' );
                    ini_set( 'log_errors' );
                    ini_set( 'error_log', SYSPATH . DS . 'logs' . DS . 'errors.log' );
                    break;
            }
            
            /**  Set's default timezone for this application    */
            date_default_timezone_set($appOptions['Timezone']);
        }
    }

    Bootstrap::init();
    var_dump($appOptions);
    

The Main.php file only contains the array appOptions (for now), i m thinking in the future adding the Database connection and Application requirements (example if you need mysql or gd modules installed). Okay now focusing on my question a little the output of the code above delivers the follow:

array(2) { ["Developer"]=> bool(true) ["Timezone"]=> string(13) "Europe/Lisbon" }
Notice: Undefined variable: appOptions in C:\EasyPHP\data\localweb\Framework\Packages\Bootstrap.php on line 33
NULL 

Note: 1st line is from the var_dump inside the init and 2nd is from outside the bootstrap class.

 

With this i assume that the require i do in the init function only includes the file for the class and not for the application over all? is this the best way of doing an bootstrap or there's an better way?

 

Thanks in advance,

Edited by criostage
static public function init ( ){
    self::$_appConfig = APPPATH . DS . 'Config' . DS . 'Main.php';
    require self::$_appConfig;
    var_dump($appOptions);
}

Are you mixing and matching variable names in using appConfig and appOptions, or is appOptions somehow set elsewhere (like in the file required through $appConfig)? I see it dumped and used for operations, but I don't see it being set. If it is, then the scope of the included file would appear to be limited to the scope of the init() function.

Edited by boompa
static public function init ( ){
    self::$_appConfig = APPPATH . DS . 'Config' . DS . 'Main.php';
    require self::$_appConfig;
    var_dump($appOptions);
}

Are you mixing and matching variable names in using appConfig and appOptions, or is appOptions somehow set elsewhere (like in the file required through $appConfig)? I see it dumped and used for operations, but I don't see it being set. If it is, then the scope of the included file would appear to be limited to the scope of the init() function.

 

 

The $AppOptions is set in the Main.php File:

<?php
    /**
     *  Array that contains the application settings to be applied at it's startup.
     */
    $appOptions = array (
        'Developer'     =>  TRUE,
        'Timezone'      =>  'Europe/Lisbon',
    );

The idea would be to apply this array to the bootstrap, in the furure i would like to add more options to that file

Edited by criostage

Functions, Methods and Classes all have their own scope.

 

Variables that are defined within them, do not exist outside of them and vice versa.

 

So i guess i was right on my first bunches, tyvm for the information and sorry for the newbish question i was only used to do small scripts this will help me in the future.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.