0

I have a structure like so:

main.php

include_once func1.php

include_once func2.php

These two files are include'd inside main.php.

I get the error below when I call a function switchboard() from func1.php inside finc2.php.

Fatal error:  Uncaught Error: Call to a member function switchboard() on null in func2.php:16
Stack trace:
#0 main.php(60): decode_func('{"auth":"...)
#1 {main} thrown in func2.php on line 16

Line 16 is where I call the function from func1.php inside func2.phpswitchboard() {}. Is there a way to fix this besides includeing func1.php inside func2.php?


func2.php

 function decode($var) {

     if() {return $var;} 
     else { $erm->switchboard('101', $var); }   
 }

func1.php

 $erm = new CLASS() {

   function switchboard($id, $var) {

     if() {}
     else {}
   }

 }
Nikk
  • 7,384
  • 8
  • 44
  • 90
  • If this isn't legacy code, consider using a framework or at least composer to manage your application in a more modern way. – Halfstop Mar 28 '17 at 17:29
  • I have setup `Exception`s inside `main.php`...that are being called in `func1.php`. Will they still work if `func1.php` is inside `func2.php`? @HankyPanky – Nikk Mar 28 '17 at 17:30
  • @HankyPanky And for the sake of expandability in the future, what if there is a third file say `func3.php`...included in main. Is there a more elegant and versatile way do organizing this? – Nikk Mar 28 '17 at 17:35
  • The function is defined, but the object that you're trying to call it from isn't an object. Show your code. – aynber Mar 28 '17 at 17:35
  • Follow @aynbar's advice that makes sense – Hanky Panky Mar 28 '17 at 17:37
  • This has nothing to do with including files.. PHP obviously complains you're trying to call the function on a null object. Also, if you want a concrete solution, you should post the shortest REAL code that causes you problems. – walther Mar 28 '17 at 17:43
  • @walther Simplified version of my code was added. Please take a look. – Nikk Mar 28 '17 at 17:57
  • @aynber Simplified version of my code was added. Please take a look. – Nikk Mar 28 '17 at 17:58
  • I've never seen a class defined that way, but apparently it's semi-valid. Try taking the parenthesis off of your declaration: `$erm = new CLASS {` – aynber Mar 28 '17 at 18:00
  • 1
    @aynber Its a new way to define anonymous classes in php 7. Just did. Get the same error though. – Nikk Mar 28 '17 at 18:03
  • Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – user3942918 Mar 28 '17 at 21:09

1 Answers1

2

That would be because you use $erm in the function decode(), yet it is not included in the function's scope (let's keep in mind that contrarily to javascript, php functions do not inherit their surrounding scope)

You can declare decode as an anonymous function and take advantage of use to inject $erm inside it, or make $erm an argument of decode.

  • Anonymous function

Just use $erm to make sure to include it inside decode's scope:

$decode = function ($var) use ($erm) {
    if() { return $var; } 
    else { $erm->switchboard('101', $var); }   
};
  • Parameter

Pass $erm like any other parameter.

function decode ($var, $erm) {
    if(false) { return $var; } 
    else { $erm->switchboard('101', $var); }   
}
  • Now I get this error `syntax error, unexpected 'use' (T_USE), expecting '{'`...what is causing this? – Nikk Mar 28 '17 at 18:38
  • In the func2.php file I have it as `function decode($var1, $var2) use ($erm)`. Same as your code above. With parenthesis. Says the error is on the line where I open the function. – Nikk Mar 28 '17 at 18:49
  • Doesn't even get to the line where I call the external function. Is there anything else I can try? – Nikk Mar 28 '17 at 18:58
  • Can you actually paste that non working code on [eval.in](https://eval.in) so that we can reproduce the error? the use keyword is a known php feature, it works. without the specific code, it's only jabs in the dark ;) – Félix Adriyel Gagnon-Grenier Mar 28 '17 at 18:59
  • Just did. Doesn't show any errors. But maybe the fact that the code is split between multiple files cannot be accounted for on eval.in. When I disable this function everything works well with my code. – Nikk Mar 28 '17 at 19:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139298/discussion-between-felix-gagnon-grenier-and-borsn). – Félix Adriyel Gagnon-Grenier Mar 28 '17 at 19:03