I have an SMF forum installed, which is a non-OO application. I'm hoping to create an additional application in a directory within the forum, that includes the SSI file from SMF and then uses the functions from within SMF, but in an Object Oriented setting.
If this isn't possible then stop reading here and please explain why, because I'm struggling to work out what's wrong here (might be the fact that I've been awake for almost 24 hours)
So this is what I have:
SMF Has a file, SSI.php - When included, this sets a load of global variables, and also sets a definition of
define('SMF', 'SSI');
I have a folder called console, which has a series of files. From index.php, I call my Servlet file, which has
require_once '../SSI.php';
class Servlet
{
public function __construct(){
}
public function processRequest(){
echo SMF;
var_dump($context) //This is the global variable that should be set
}
}
This outputs SSI and then an undefined variable error.
This is probably really obvious, but why is the definition being set and not the context global? If I do this outside of a class, it works.
Thanks!
-Edit-
Just to explain why this isn't a duplicate of the linked question ... I don't really think it needs much explanation other than this is a specific question regarding a specific scope problem and a specific error, tailored to my application which is a mixture of OO and procedural programming, which lead to some confusion.
My question was not "What is a variable scope?"
Thanks.
as stated in my comment you can put
global $context;prior to calling it, however this is REALLY bad practice and can lead to huge problems in large projects. The better option is to pass in the required variables to the method being called, for instance:The reason it wasn't working in your code example is because when a class is instantiated, it creates a parallel scope to the global scope, thus your
$contextvariable doesn't exist inside the class unless you put it there through a parameter or you tell php which scope to look in for the variable by using theglobalmodifier.