Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTravis Coats
15,058 Pointsinclude_once vs static::class method include
I am at my wits end! I have been dabbling with OOP to create a nice solid base of classes I can use to develop some upcoming PHP apps but I have run into an issue that I can't wrap my head around. Basically, I have a Page class which takes care of pretty much everything to do with a webpage. On my test index page I am calling a static method load_part():
public static function load_include($string) {
include_once INCLUDES . $string;
}
INCLUDES is a constant set in my configuration file. The INCLUDES constant is being evaluated correctly. The problem I am running into is that if I have a variable declared in the global scope, i.e. a font, and I have code in the include to receive that value, I get a fatal error saying that the variable is undefined! If I replace the static function call with a regular old include_once statement on the test index page, it interprets the variable just fine.
Here's a snippet to elaborate my situation:
Test Index page: <pre> <?php require_once '../core/config/dir_lib.php'; require_once INITIALIZE; ?> </pre> <pre> <!DOCTYPE html> <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>[black_Box] by Zen Perfect Design</title> <meta name="description" content="[black_Box] is a proprietary software developed and maintained by Travis Coats, lead developer of Zen Perfect Design"> <meta name="viewport" content="width=device-width, initial-scale=1">
<?php Page::load_include('css.html.php');?>
<script src="<?php echo BASE_URL;?>js/vendor/modernizr-2.6.2.min.js"></script>
</head>
</pre>
The included file looks like:
<pre>
<link rel="stylesheet" href="<?php echo BASE_URL;?>css/normalize.css">
<link rel="stylesheet" href="<?php echo BASE_URL;?>css/bootstrap.min.css">
<link rel="stylesheet" href="<?php echo BASE_URL;?>css/font-awesome.min.css">
<link rel="stylesheet" href="<?php echo BASE_URL;?>css/animate.css">
<link rel="stylesheet" href="<?php echo BASE_URL;?>css/blackbox.css">
<?php
for($f = 0; $f < Font::get_num_fonts();$f++ ) {
$font[$f]->echo_link();
}
for($c = 0; $c < Font::get_num_fonts();$c++ ) {
$font[$c]->echo_css();
}
?>
</pre>
It says that font is undefined, but it is defined in the initial INITIALIZE include which looks like this:
<pre> // Classes include_once CLASSES . 'database.php';
include_once CLASSES . 'font.php';
// Initialize Fonts
$font[] = new Font('Lato'); // All basic elements styled leave this alone
/* Set any specific fonts to elements you would like by creating a new Font object
* Additional fonts will be numbered starting at 1 using the example below you can
* copy and past those two lines as many times as neccessary just replace the number in
* brackets with the next sequential number i.e. 2, 3, 4, 5 etc.
* You can add as many custom font styles as you like. Just uncomment out the lines
* to get started!
*/
//$font[] = new Font('Special Elite');
//$font[1]->set_elements('h1, h2, h3, h4, h5, h6');
include_once CLASSES . 'page.php';
// Initialize Page
$page = new Page($_GET);
</pre> Any ideas? I am losing it. :|
Travis Coats
15,058 PointsI know it has something to do with the scope, but I am worried about how I will be able to use my static functions to retrieve parts and views from the filesystem without the ability to evaluate global variables. Is there a work around? the end goal is that the page class will determine which file to retrieve based on specific GET data. However, some global variables like the font controller and other possibles might need to flow into these 'php files' when they are included. Otherwise, i guess I have to put all the logic in the file included.
Travis Coats
15,058 PointsBasically, I know there are frameworks that do includes in a similar manner, so how do they get the variables processed in the include file when called from a class method? include_once() does evaluates variables instantiated before the function call in the file included, but my situation shows that the scope is centered in the class method. I guess the better question is how does PHP include_once() know to evaluate variables that have been declared outside the function?
Travis Coats
15,058 PointsTravis Coats
15,058 PointsP.S. I have no idea why the code highlighting skips parts of the code on the forum post. I hope it is readable.