I posted code in the first entry for this series that allowed you to test and read XenForo code. I called the file connectXenForo.php. If you haven't read the first entry for this series then please do so now and return after you have set up your local environment.
Today we'll look at the index.php file loaded by XenForo. The contents of this file should look very familiar to you because we put a modified version of it in the connectXenForo.php file. The contents are also similar to the contents of the index.php file in the /install directory.
In the simplest of terms, the index.php file initializes the XenForo software.
Line 1
We start the file, as we did with previous files, by adding an opening PHP tag. There is no matching closing tag in the file.
Line 3
A call to microtime with an argument of true passed inside the parenthesis. Microtime will be used in a later line of code as an argument for setting the page_start_time.
By default, microtime returns a string, however when the argument is set true then a float is returned. A float is shorthand for floating point numbers.
We can test this by creating a file, name it start.php. Add the following contents.
When you load the start.php in the browser then the word float and a long number with a decimal follows.
Line 4
The function dirname always ticks me off. I have to remember that dirname returns a path passed as an argument. In this example, the $fileDir is set so that the directory information can be used multiple times without repeating dirname(__FILE__).
The equivalent in PHP 5.3 and greater is to use __DIR__ in place of dirname(__FILE__). This is two underscores on both sides of the DIR.
For those of you new to PHP, you'll start to see that a variable is started with the $ symbol. The variable is a holder of information, and that information can vary or change.
Line 6
The Autoloader.php file is a means to avoid a long list of include or require statements. Line 6 adds the contents of the Autoloader.php file.
Line 7
The magic in the Autoloader file is converting the class names to the file structure. For example, the class name XenForo_Application resides in the file Applications.php which is located in the directory main XenForo installation/library/XenForo. We can dive deeper into this file another time.
The getInstance method is the second to last in the Autoloader file. This method creates an instance of the class.
Line 9
At the top of the Application file, found in the library/XenForo directory, you will find line after line of variables .
From the Zend Framework manual.
You should find the initialize method near line 287. Among other things, it calls the beginApplication method.
Line 10
The page_start_time exists in 15 different files: community index, install index, and debug. Unlike the initialize above, the set is not a method in the XenForo/Application.php file. This might throw you for a moment. Look at line 11 in the Application file.
The class XenForo_Application is extending the Zend_Registry class. This is called Inheritance. The class XenForo_Application has all of the features of the Zend_Registry class plus anything new specified in the file.
For now, just think of inheritance as a means to reuse code without retyping it; and inheritance allows the developer to make specific changes for a particular object.
You will see XenForo extending classes in many places.
Line 12
This line is an instantiation, or creating of an object. Let's create a new file called fc.php so that we can see what is inside that object.
Browse to the fc.php page in your browser and you should see something similar to the following.
Once again we see arrays, or lists. Take a moment or two and look at the first array followed by (size =16). The array starts at 0 and moves to 15, thus containing 16 items. To call the first then we would reference the 0 and the second item would be 1.
If we dig deeper then we'll see each string is also followed by lengths in parentheses. Count the letters within the single quotes and you'll see the number of letters match the length.
Line 13
This is a call to the method run() which resides on line 117 in the file /library/XenForo/FrontController.php
We can spend time in the next entry on instantiation of an object. If you are new to PHP then review many of the concepts in the first two entries. I've thrown a lot at you.
Conclusion
We've done a massive amount of concepts in analyzing these few lines. Hopefully you are starting to think about some of the concepts outside of reading this series: PHP starting tag, use of arrays and objects. Don't worry, start out small.
Homework
Your next task is to create a file called array.php which will contain an array of items. Do a var_dump and study what is returned in your browser.
Let me know in the comments how you are doing and what you see in the var_dump.
Today we'll look at the index.php file loaded by XenForo. The contents of this file should look very familiar to you because we put a modified version of it in the connectXenForo.php file. The contents are also similar to the contents of the index.php file in the /install directory.
In the simplest of terms, the index.php file initializes the XenForo software.
PHP:
<?php
$startTime = microtime(true);
$fileDir = dirname(__FILE__);
require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');
XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);
$fc = new XenForo_FrontController(new XenForo_Dependencies_Public());
$fc->run();
Line 1
We start the file, as we did with previous files, by adding an opening PHP tag. There is no matching closing tag in the file.
Line 3
A call to microtime with an argument of true passed inside the parenthesis. Microtime will be used in a later line of code as an argument for setting the page_start_time.
By default, microtime returns a string, however when the argument is set true then a float is returned. A float is shorthand for floating point numbers.
We can test this by creating a file, name it start.php. Add the following contents.
Code:
<?php
require_once 'connectXenForo.php';
var_dump($startTime);
When you load the start.php in the browser then the word float and a long number with a decimal follows.
Line 4
The function dirname always ticks me off. I have to remember that dirname returns a path passed as an argument. In this example, the $fileDir is set so that the directory information can be used multiple times without repeating dirname(__FILE__).
The equivalent in PHP 5.3 and greater is to use __DIR__ in place of dirname(__FILE__). This is two underscores on both sides of the DIR.
For those of you new to PHP, you'll start to see that a variable is started with the $ symbol. The variable is a holder of information, and that information can vary or change.
Line 6
The Autoloader.php file is a means to avoid a long list of include or require statements. Line 6 adds the contents of the Autoloader.php file.
Line 7
The magic in the Autoloader file is converting the class names to the file structure. For example, the class name XenForo_Application resides in the file Applications.php which is located in the directory main XenForo installation/library/XenForo. We can dive deeper into this file another time.
The getInstance method is the second to last in the Autoloader file. This method creates an instance of the class.
Line 9
At the top of the Application file, found in the library/XenForo directory, you will find line after line of variables .
From the Zend Framework manual.
You should find the initialize method near line 287. Among other things, it calls the beginApplication method.
Line 10
The page_start_time exists in 15 different files: community index, install index, and debug. Unlike the initialize above, the set is not a method in the XenForo/Application.php file. This might throw you for a moment. Look at line 11 in the Application file.
Code:
class XenForo_Application extends Zend_Registry
The class XenForo_Application is extending the Zend_Registry class. This is called Inheritance. The class XenForo_Application has all of the features of the Zend_Registry class plus anything new specified in the file.
For now, just think of inheritance as a means to reuse code without retyping it; and inheritance allows the developer to make specific changes for a particular object.
You will see XenForo extending classes in many places.
Line 12
This line is an instantiation, or creating of an object. Let's create a new file called fc.php so that we can see what is inside that object.
Code:
<?php
require_once 'connectXenForo.php';
$fc = new XenForo_FrontController(new XenForo_Dependencies_Public());
var_dump($fc);
Browse to the fc.php page in your browser and you should see something similar to the following.
Code:
object(XenForo_FrontController)[3]
protected '_dependencies' =>
object(XenForo_Dependencies_Public)[10]
protected '_dataPreLoadFromRegistry' =>
array (size=16)
0 => string 'routesPublic' (length=12)
1 => string 'nodeTypes' (length=9)
2 => string 'bannedIps' (length=9)
3 => string 'discouragedIps' (length=14)
4 => string 'styles' (length=6)
5 => string 'displayStyles' (length=13)
6 => string 'userBanners' (length=11)
7 => string 'smilies' (length=7)
8 => string 'bbCode' (length=6)
9 => string 'threadPrefixes' (length=14)
10 => string 'userTitleLadder' (length=15)
11 => string 'reportCounts' (length=12)
12 => string 'moderationCounts' (length=16)
13 => string 'userModerationCounts' (length=20)
14 => string 'notices' (length=7)
15 => string 'userFieldsInfo' (length=14)
public 'notices' =>
array (size=5)
'showUpgradePendingNotice' => string 'notice_upgrade_pending' (length=22)
'showBoardClosedNotice' => string 'notice_board_closed' (length=19)
'isAwaitingEmailConfirmation' => string 'notice_confirm_email' (length=20)
'isEmailBouncing' => string 'notice_email_bounce' (length=19)
'showCookieNotice' => string 'notice_cookies' (length=14)
protected '_viewStateChanges' =>
array (size=0)
empty
protected '_defaultTemplateParams' =>
array (size=0)
empty
protected '_request' => null
protected '_response' => null
protected '_sendResponse' => boolean true
Once again we see arrays, or lists. Take a moment or two and look at the first array followed by (size =16). The array starts at 0 and moves to 15, thus containing 16 items. To call the first then we would reference the 0 and the second item would be 1.
If we dig deeper then we'll see each string is also followed by lengths in parentheses. Count the letters within the single quotes and you'll see the number of letters match the length.
Line 13
This is a call to the method run() which resides on line 117 in the file /library/XenForo/FrontController.php
We can spend time in the next entry on instantiation of an object. If you are new to PHP then review many of the concepts in the first two entries. I've thrown a lot at you.
Conclusion
We've done a massive amount of concepts in analyzing these few lines. Hopefully you are starting to think about some of the concepts outside of reading this series: PHP starting tag, use of arrays and objects. Don't worry, start out small.
Homework
Your next task is to create a file called array.php which will contain an array of items. Do a var_dump and study what is returned in your browser.
PHP:
<?php
$list = array( '', 1, 3, 5, '8', 10);
var_dump($list);
Let me know in the comments how you are doing and what you see in the var_dump.