Jump to content


Photo

Class Loading


  • Please log in to reply
9 replies to this topic

#1 Gazley

Gazley

    Sr. Member

  • Members
  • PipPipPipPip
  • 243 posts
  • 39

  • LocationManchester, UK

Posted 12 August 2012 - 07:08 AM

Hi there,

I created a static helper class for some utility functions (gwutils.php).

When I try and access it:

$summaryImg = gwUtils::GetImageByCategory('summary', $summary);

PW is picking this up through spl_autoload_register and obviously throwing an error because it isn't registered.

What is best practice around creating/registering/loading site based custom classes?

Thanks!

#2 Soma

Soma

    Hero Member

  • Moderators
  • 3,397 posts
  • 1928

  • LocationSH, Switzerland

Posted 12 August 2012 - 07:34 AM

Not sure what exactly you're doing but you could create a modules that is autoloaded and provides functions to use everywhere in PW.

Something like this:

class HelpersModule extends WireData implements Module {

    /**
     * getModuleInfo is a module required by all modules to tell ProcessWire about them
     *
     * @return array
     *
     */
    public static function getModuleInfo() {

        return array(
            'title' => 'Helper Functions',
            'version' => 100,
            'summary' => '',
            'href' => '',
            'singular' => true,
            'autoload' => true
            );
    }


    public function init() {
        $this->setFuel("helpers", $this);
    }


    public function getPosts() {
        return wire("pages")->get("/posts/")->children();
    }


}

Then you can do in your templates:
$posts = $helpers->getPosts();

There's variations of this but it should be simple to do. Also you could also make it not autoload and load it on demand.

$helpers = $modules->get("HelpersModule"); // load module
$posts = $helpers->getPosts();

Edit: wow everytime I edit the post the indentation get lost, that's new to me and pretty annoying... :D

@somartist | modules created | support me, flattr my work flattr.com


#3 Gazley

Gazley

    Sr. Member

  • Members
  • PipPipPipPip
  • 243 posts
  • 39

  • LocationManchester, UK

Posted 12 August 2012 - 07:57 AM

Hey Soma,

Thanks for pointing me towards a module for this requirement. I can see me creating lots of modules once I get the hang of it!

BTW, the tips you gave me for filtering my image categories have worked really well! PW is so powerful :)

Best,

--Gary

#4 Soma

Soma

    Hero Member

  • Moderators
  • 3,397 posts
  • 1928

  • LocationSH, Switzerland

Posted 12 August 2012 - 08:02 AM

You should get into modules pretty soon! They're easy as pie and powerful! ;)

PS: Glad to hear it worked out, thanks for mention it.

@somartist | modules created | support me, flattr my work flattr.com


#5 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3380

  • LocationAtlanta, GA

Posted 12 August 2012 - 09:03 AM

I agree with Soma's reply. Also wanted to mention you could always register your own autoload function too.

In my case, I either go the route of including a "tools.inc" file at the top of my templates that has all my shared functions (and/or classes if needed), or I use the module route as Soma mentioned.

#6 Gazley

Gazley

    Sr. Member

  • Members
  • PipPipPipPip
  • 243 posts
  • 39

  • LocationManchester, UK

Posted 12 August 2012 - 09:07 AM

The file extension is .module - will PW get upset if it has a PHP extension too? My IDE doesn't give me the usual "help" if the file hasn't a PHP extension! :)

#7 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3380

  • LocationAtlanta, GA

Posted 12 August 2012 - 09:22 AM

PW uses the .module extension for the same reason Drupal does... so that the file can quickly be identified (by PW's module loader) as a file containing a plugin module rather than some other PHP file. We've had a request to make it also support .module.php, so will likely be adding this shortly in the dev branch. But for now my suggestion would be to set your editor to recognize .module as a PHP file. For instance, I use VIM and place this in my .vimrc file:

set nowrap
syntax on
au BufRead,BufNewFile *.module set filetype=php


#8 Gazley

Gazley

    Sr. Member

  • Members
  • PipPipPipPip
  • 243 posts
  • 39

  • LocationManchester, UK

Posted 12 August 2012 - 09:26 AM

I've created the module (based on Soma's example) and placed it in site/modules (with a .module extension only). However, it doesn't seem to get loaded. Is there something you have to do to tell PW to load site based modules?

Thanks.

#9 Gazley

Gazley

    Sr. Member

  • Members
  • PipPipPipPip
  • 243 posts
  • 39

  • LocationManchester, UK

Posted 12 August 2012 - 09:30 AM

Hah, what a Muppet - you have to install it through the Module's panel! Woot! :)

I'm having (many) Senior Moment(s) ;)

#10 Gazley

Gazley

    Sr. Member

  • Members
  • PipPipPipPip
  • 243 posts
  • 39

  • LocationManchester, UK

Posted 12 August 2012 - 11:13 AM

@ryan - added .module to my PHP pattern and all is well, IDE and more importantly me ... happy again :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users