We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • If I want to create a php class, which I include using:
    if (!class_exists("ListMenuX")) { include "assets/snippets/listmenux/listmenux.inc.php"; }

    how can I then, within the included class, access my $etomite instance? Currently, when trying to call $etomite->getActiveChildren() from within a function in my class like so:
    function getChildList($parentId)
    {
    $listArray = false;
    if (!$parentId===false) { $listArray = $etomite->getActiveChildren($parentId); }
    return $listArray;
    }

    I get the error:
    Fatal error: Call to a member function on a non-object in /home/sallen/public_html/thevirtualhandshake/assets/snippets/listmenux/listmenux.inc.php on line 53

    Any suggestions on the proper way to approach this in the framework?
      • 32963
      • 1,732 Posts
      Hi,

      Here's a quick solution:

      1) you can use the global keyword to make $etomite global

      2) you can pass the $etomite variable to the class constructor and assign it as a variable of the class:

      $lm = new ListMenuX($etomite);

      from inside the constructor:

      function ListMenuX($etomite) {
      $this->etomite = $etomite;
      }
        xWisdom
        www.xwisdomhtml.com
        The fear of the Lord is the beginning of wisdom:
        MODx Co-Founder - Create and do more with less.
      • Thanks xwisdom, that worked like a charm. I had actually tried that just before posting, but had accidentally tried to call the function with
        $this->$modx->getActiveChildren($parentId)

        which of course didn't work, since I included the $ before modx.
        shockedops: