We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23054
    • 62 Posts
    I am very confused, because I find no error on this

    snippet
    include_once('file_class_a.php');
    


    file_class_b.php
    <?
    
    class B
    {   function functionB()
        { 
        }
    
    }
    $b = new B();
    ?>


    file_class_a.php
    <?
    include_once("file_class_b.php");
    
    class A
    {   function functionA()
        { global $b;
    
         b->functionB();
        }
    
    }
    $a = new A();
    ?>


    leads to "Call to a member function on a non-object ... " by using $a->functionA();
    I have no idea what is wrong about the global definition.
    • Hi persepolis,

      Could it be the missing dollar-sign before the object reference wink :
      b->functionB();
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 23054
        • 62 Posts
        Quote from: garryn at Jul 17, 2006, 07:57 PM

        Hi persepolis,

        Could it be the missing dollar-sign before the object reference wink :
        b->functionB();


        No, that is a mistake in the example, but not in the origin source code
        • Okay, is the code for Class A inside a snippet? If it is, then you will need to define $b as global at snippet level, just before the include_once().
            Garry Nutting
            Senior Developer
            MODX, LLC

            Email: [email protected]
            Twitter: @garryn
            Web: modx.com
            • 23054
            • 62 Posts
            Quote from: garryn at Jul 17, 2006, 08:37 PM

            Okay, is the code for Class A inside a snippet? If it is, then you will need to define $b as global at snippet level, just before the include_once().

            This could be the solution - I will try tomorrow and report.
            Thanks smiley
              • 23054
              • 62 Posts
              Your solution works.

              Anyway - I wonder why this has to be done - I don’t understand ?

              I can using the global $modx-variable without defining it again as global in a snippet - how is this be done ?

              I am confused huh