-1

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.

Chris Evans
  • 1,235
  • 10
  • 14
  • 1
    try putting `global $context;` before your `var_dump($context);`. This is a really poor way of doing it, but it will get the job done provided the global variables are not inside a lower scope. the better option is to pass in the variables needed upon calling the method – iam-decoder Nov 04 '15 at 07:56
  • Oh man I need sleep, that was so obvious and now I am kicking myself so hard. Thanks, haha. – Chris Evans Nov 04 '15 at 07:58

2 Answers2

2

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:

<?php
require_once '../SSI.php';

class Servlet
{

    public function __construct(){

    }

    public function processRequest($context){
        echo SMF;
        var_dump($context);
    }
}
$servlet = new Servlet();
$servlet->processRequest($context);

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 $context variable 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 the global modifier.

iam-decoder
  • 2,554
  • 1
  • 13
  • 28
  • Very good, thank you so much. I'm fairly new to OOP and so I derped when it came to the scope and didn't think to just pass it as a parameter. – Chris Evans Nov 04 '15 at 11:54
0

You have a missing ' between your defined variable content and the bracket. Maybe it's a copy & paste error, but just to be sure..

Johannes
  • 1,478
  • 1
  • 12
  • 28