How to name my plugin class?

Status
Not open for further replies.

Floris

I'm just me :) Hi.
Staff member
Joined
Jan 1, 2001
Messages
60,096
This question has been asked a few times, and there's no straight or one liner answer to it, but I will try to explain. Well, I am still learning myself, so let's hope I am not wrong.

Basically, I recommend people to make a unique collective directory to put their own custom add-on code in. Ragtek for example uses the directory ragtek/ and I use the directory xenfans/. Why? Well, if we both have a Listener.php file, we now do not run the risk to overwrite it. Plus, it's just easier to find a plugin in the directory listing. Oh and well, it just looks organized.

At this point we have library (by xenforo) and say XenFans/ by xenfans.com, the path is: upload/library/XenFans/

The second step is what I call 'being obvious'. If you're writing a plugin that adds an extra tab to the navigation bar, calling it 'extra' or 'extra tab' as add-on name is simple, short, and obvious. You can of course be creative and call it something like Tabr (very web 2.0 right?). I will leave that up to you. Assuming I might create other add-ons that are "extra" things, I might go with extra tabs over just extra.

At this point we have library/XenFans/ and within it the add-on name ExtraTab/ The path is: upload/library/XenFans/ExtraTab/

It's arguable if you desire to put your Listener.php file within the Listener/ directory first. We could have a huge discussion about it. Personally I think this through like this: Am I going to make a small or bigger add-on? Will it be much bigger in the future? If not, perhaps just having a Listener.php file is enough. Otherwise if I might have more things like routes, controllers, data writers, and what not, if not only for organizational reasons I like to put the file within the Listener/ directory.

For the sake of the name of the class- example, I will put it in the directory.

At this point we have library/XenFans/ExtraTab, and within it the Listener/ directory where I put the Listener.php file. You can also name this file Banana.php or Index.php I am going to call it Index.php. So the path is: library/XenFans/ExtraTab/Listener/Index.php

The next step is to create a new file, start with <?php and start the class { }

The name of the class can't be made up now. It's basically following the directory path we just created. Minus upload/library and minus the .php file extension.

So you're extending it with XenFans/ExtraTab/Listener/Index.php (and capitalization matters)
So that's directory_directory_directory_filename.
So the class is: XenFans_ExtraTab_Listener_Index

Your Index.php file within the Listener/ directory should now look like this:

PHP:
<?php
// Comment: My Listener file for the add-on 'Extra Tab'
class XenFans_ExtraTab_Listener_Index
{
// Comment: Your code goes here, in between the curly brackets
// Comment: For example: public function insertTemplate() { // code }
}

So "how do I know what to call my class" question's answer is: That depends on your .php file, and where within library it's stored.

If the plugin's Listener file is called boom.php and it's not within a directory, just directly inside library, the class is named like this:
class boom { // code here }

If the plugin's file is called Checkpoint.php and it's within the directory Route, which is inside the library/ directory, the class is named like this:
class Route_Checkpoint { // code }

Extending the example ..

I just used the Listener file as an example.
Now, if you go to admin.php, click on the Development tab, then on Code Event Listener, click on the top right button: Create new even listener, and for example on template_hook, the page will have 2 input fields called Execute Callback, with on the left the class, and the event on the right.
The class we now know. XenFans_ExtraTab_Listener_Index
The event is the (for example) public function insertTemplate, so we use insertTemplate
You can make the other appropriate selections, such as add-on name, and what not. And click save. (Which will either work, or not. When not, it's usually a) you didn't use the right class name, or b) the .php file is not on the server, or in the wrong spot. The class has to exist and xenforo must be able to find it.

I hope this helped a few people.
 

Fuhrmann

OMG Member
Joined
Oct 27, 2011
Messages
93
Nice one, again!

I dont know if it a coincidence, but right now I am writing a basic tutorial too, that explain more or less what you have explained here!
 

Fuhrmann

OMG Member
Joined
Oct 27, 2011
Messages
93
I read it again. This is a 'must read' article for all people starting coding in XenForo.
 
Status
Not open for further replies.
Top