Zikula: A Flexible Open Source Content Management System
home | forum | contact us

Dizkus

Bottom
Help With: Utilizing Classes in Module Development
  • Posted: 04.05.2006, 23:14
     
    randomblink
    rank:
    Helper Helper
    registered:
     February 2003
    Status:
    offline
    last visit:
    06.03.08
    Posts:
    233
    Alrighty then...
    I have, what I hope to be a SIMPLE question for the PostNuke gurus...

    I would like to utilize some Classes in my module design.
    However, I absolutely HATE to include or include_once in PostNuke files... (I haven't had to do it yet!)
    Surely there is another way...
    I have considered that if I were to create a file called: pnThisClassAPI.php
    Then I could get it included by: pnModAPILoad( 'ModuleName', 'ThisClass' )

    BUT I am not sure of that, because I am perusing the new Example module in the .762 PostNuke... and there is NO use of phModAPILoad? Is it deprecated? No longer needed? I am lost...

    Anyway, if this would work... Or if there is another way? Could someone let me know how to access my own created classes in a module I am designing WITHOUT using include() or include_once()???

    Thanks in advance...

    --
    That was a hell of a thing...
    -------------------------------------
    Galaxy Quest
  • Posted: 05.05.2006, 00:08
     
    ctimmer
    rank:
    Helper Helper
    registered:
     February 2003
    Status:
    offline
    last visit:
    11.06.08
    Posts:
    226
    Haven't tried this but...
    For example:
    In the pnuserapi directory create a file called 'getmyclassobject.php'. In this file place your class definition and an the apporpriately named function called ..._getmyclassobject that creates and returns an instance of myclass.

    You should be able to retrieve the myclass instance through the PN compliant user API call.
  • Posted: 05.05.2006, 15:50
     
    Lobos
    rank:
    Professional Professional
    registered:
     December 2002
    Status:
    offline
    last visit:
    24.08.08
    Posts:
    1588
    Interesting question - I too use a lot of classes in my mods and have been require_once 'ing them, but on a few occasions I have noticed that the class seems to load anyway... strange, but this gives me a feeling that PN has some way of managing them, I put my classes in a directory called pnclasses so maybe PN autoloads these?

    -Lobos

    --
    -Lobos
    Professional PHP Framework Services: Concept, Development and Deployment
  • Posted: 05.05.2006, 16:23
     
    rgasch
    rank:
    Professional Professional
    registered:
     January 2003
    Status:
    offline
    last visit:
    03.12.08
    Posts:
    569
    You *really* want to take a look at the PN 0.8 codebase, specifically at PNObject.class.php and PNObjectArray.class.php in includes/pnobjlib.

    These 2 files define a component framework in which your class (if properly defined) automatically knows how to select/insert/update/delete the underlying data object. Together with the Loader (Loader.class.php) this gives you a component model where it's easy to build a structure where the appropriate classes are only loaded on demand, therefore minimizing the (often unnecessary) loading of all sorts of library classes.

    It's a bit of a change from the traditional PN development model, but it's quite efficient, gives you an orderly & spearated code structure and lets you manage your data object through a generic object interface. All in all, IMHO quite an improvement to the traditional way of handling mulitple object implementations.

    If you want to try this on the 0.76x codebase (rather than resorting to the 0.8 codebase), you can download the Openstar distribution from http://openstar.postnuke.com/. It's available as a full distro or as an addon package to 0.762. In 0.8 all the stuff will be integrated into PN out-of-the-box but for the 0.76x series it's an addon.

    For reference: most the v4b* modules which are part of the Openstar distro use this programming model so there's lots of sample code in there you can look at to get an idea of how this works in detail.

    Greetings
    R
  • Posted: 05.05.2006, 17:51
     
    Lobos
    rank:
    Professional Professional
    registered:
     December 2002
    Status:
    offline
    last visit:
    24.08.08
    Posts:
    1588
    Thanks for your info rgasch.

    Is it possible to implement existing classes with this model? ie, say if I download a class from phpclasses.org, will I be able to implement the class without hacking it?

    -Lobos

    --
    -Lobos
    Professional PHP Framework Services: Concept, Development and Deployment
  • Posted: 05.05.2006, 20:13
     
    randomblink
    rank:
    Helper Helper
    registered:
     February 2003
    Status:
    offline
    last visit:
    06.03.08
    Posts:
    233
    Whereas I respect the fact that you seem to know alot about the .8 codebase, and I do not wish to seem rude, could you couch your reply in a way that might answer my question in simpler terms?

    Are you saying that if I name my classes: classname.class.php that my module will autoload them?
    I am really confused...
    I am going to dig around the .8 structure like you said and see if I find my answers there...
    But I really am just looking for the step-by-step process to utilizing classes in an effecient manner within my module(s)...



    edited by: randomblink, May 05, 2006 - 09:13 AM

    --
    That was a hell of a thing...
    -------------------------------------
    Galaxy Quest
  • Posted: 05.05.2006, 20:45
     
    rank:
    Moderator Moderator
    registered:
     March 2002
    Status:
    offline
    last visit:
    26.08.08
    Posts:
    7720
    In the current .76x non Openstar code you need to manually load your classes via require_once.

    In .8x you get a class loader so you can use

    Code

    Loader::loadClassFromModule('<mymod>', '<myclassname>');


    This code will load modules//classes/pnmyclassname.class.php'.

    The loader has a number of other useful methods for dealing with load of files.

    -Mark
  • Posted: 05.05.2006, 22:00
     
    rgasch
    rank:
    Professional Professional
    registered:
     January 2003
    Status:
    offline
    last visit:
    03.12.08
    Posts:
    569

    Quote

    Is it possible to implement existing classes with this model? ie, say if I download a class from phpclasses.org, will I be able to implement the class without hacking it?

    Yes. You can use the

    Code

    Loader::loadFile($fileName, $path=null)

    API to load any file, regardless of wether it's a class or a normal file (this API is basically a wrapper around include_once). The loadClassFromModule() API that Mark pointed out, makes some assumptions about directory structure and class naming for classes which explicitly belong to a specific module.

    Greetings
    R
  • Posted: 05.05.2006, 22:08
     
    rgasch
    rank:
    Professional Professional
    registered:
     January 2003
    Status:
    offline
    last visit:
    03.12.08
    Posts:
    569

    Quote

    Are you saying that if I name my classes: classname.class.php that my module will autoload them?

    No, classes do not get autoloaded (since that would potentially mean that you load a lot of classes you don't need). You still need to load them explicitly.

    Quote

    But I really am just looking for the step-by-step process to utilizing classes in an effecient manner within my module(s)...

    The answer to this question depends on what your classes implement. If you use your classes to implement table/object persistence & logic (ie: insert/delete operations, validation checks, etc.) then I would suggest you take a look at the loadClassFromModule() API (the one Mark pointed out) as it's designed to handle such classes intelligently in conjunction with the component model provided by the PNObject class/structure.

    If your classes are simple util classes you downloaded from somewhere that you use in your code, then using include() (or require()) in 0.7x will do fine (in 0.8 you'd probably want to use the Loader::loadFile() API for those).

    If you're looking at the PN 0.8 codebase, take a look at the categories module, which uses the new PNObject model to implement the persistence around the (PN)Category class.

    Greetings
    R

Extensions Moderation

Main Menu

Extensions Database

Documentation

Development

Login

Donate to Zikula