Home » Documentation » Add functionality to PeoplePods with registerMethod

Add functionality to PeoplePods with registerMethod

As of 0.8, plugin pods can add functionality to core classes using the registerMethod() class method. This allows PeoplePods to be extended without changing the library code, and thus allows the system to be upgraded without overwriting custom additions.

The registerMethod() function should only be used within a methods.php plugin file, as described here. Using it outside of the methods file will cause your entire website to explode.

In addition, registerMethod() is a class method, which means it is called directly on a class, not on a specific object. This way, the method is available to all objects of that class.

function myMethod($POD) {
// do stuff.
}

PeoplePod::registerMethod('myMethod');

function myPersonMethod($person) {
// do stuff.
}

Person::registerMethod('myPersonMethod');

function myContentMethod($content) {
// do stuff.
}

Content::registerMethod('myContentMethod');

After being registered, the method may be used on any object of that class.

// create a piece of content
$doc = $POD->getContent();

// call the custom method
$doc->myContentMethod();

Methods that are added in this manner must take as their first parameter an object which represents the object upon which the method was called. That is, the first parameter is the object that would otherwise be referenced using the $this variable:

// in handler.php or other pod code:
$doc->sampleFunction();


// in methods.php
function sampleFunction($content) {
  // $content == $doc
}

Content::registerMethod('sampleFunction');

You may also specify additional parameters after the object:

// in handler.php
$doc->sampleFunction2('foo');

// in methods.php
function sampleFunction2($content,$message) { 
// ...
}
Content::registerMethod('sampleFunction2');

Note, methods added in this way are only available if the pod is enabled!

In some circumstances, such as when overriding core functions or to avoid namespace collisions, you call your new method by a name that is different from the actual function name.  In this case, you can pass a second parameter to registerMethod which will be used instead of the real name.

function functionWithALongUglyName($content){
...
}
Content::registerMethod('functionWithALongUglyName','foo');

// $content->foo() will now execute functionWithALongUglyName($content)
  • Discuss This Document

No comments have been posted yet.

Download Download the latest version of PeoplePods!

0.9 Latest Version:
Release Notes

Join the PeoplePods developer network and get direct access to documentation, additional plugins, and our forums!

  Already Registered? Login

Recent Posts from Our Blog

Version 0.9 is here!

The latest version of PeoplePods is now available for download! This version features a drastically revamped theme which is now valid HTML5, a completely rewritten JSON-powered API, many...

Ben Brown on how running a community is like throwing a giant, never-ending party

An interview I did with OpenSource.com is now online!  Read it here. In it, I discuss how running an online community is like throwing a giant, never-ending party, how open source techniques...