We build. You grow.

Get best community software here

Start a social network, a fan-site, an education project with oxwall - free opensource community software

Include custom php library | Forum

Topic location: Forum home » Support » General Questions
Marcus
Marcus Apr 27 '20

Hi folks is there a way I can include in my plugin a custom library?


include('library/run.php');

Patricia Zorrilla Leader
Patricia Zorrilla Apr 27 '20

Yes, but you have to do it with require_once()


https://www.w3resource.com/php/statement/require_once.php
Marcus
Marcus Apr 27 '20
Thanks Patricia. Last question. Any special place to put the library in? I put it in my pluging's static folder. 
Patricia Zorrilla Leader
Patricia Zorrilla Apr 27 '20

Yes, you can put it wherever you want as long as the path points correctly.

Remember that until this sentence is executed, this library is not available.

It is not a good practice to put it in init.php of your plugin if it is not essential because the init.php of all plugins are executed when loading any OxWall page and maybe in that page nothing of your plugin is needed and this affects unnecessarily the speed of the whole web and the memory consumption.

If the library has already been read, successive attempts to read it do not consume memory or time.



dave Leader
dave Apr 27 '20

I have seen a lib folder in other plugins so maybe make a lib folder and put it in there.   I am looking at this plugin now that has a lib folder, i think oxwall already excepts the lib folder without using include.   I will let you know after i poke around a bit in the plugin.


Nope no include required that i can see, you just put it in a lib folder in your plugin and the file should be in class format and then you can refer to it.


The Forum post is edited by dave Apr 27 '20
Marcus
Marcus Apr 27 '20
Thanks guys so much. 
Marcus
Marcus May 5 '20

Hi folks! Dave baby could you help me out I cant seem to make this work man. Where should I put the lib directory? Also class name should it be an instance of the plugins key like other class files?


Im trying to add this library:


$getID3 = new getID3;

Class 'getID3' not found!

The Forum post is edited by Marcus May 5 '20
dave Leader
dave May 9 '20

Where or how did you define your class, do you have a class with that name?  


for example in classes folder do you have a file for that class with class name


class PLUGINNAME_CLASS_Getid3


remember that uppercase matters in class names


Then in your activate file you can have something like



if(class_exists('PLUGINNAME_CLASS_Getid3'))
{
 require_once dirname(__FILE__) . DS .  'classes' . DS . 'getid3.php';

 $getID3 = new PLUGINNAME_CLASS_Getid3();

 $getID3 = whatever function name in that class();

}



When you said library i was thinking you wanted to add third party library files.




Marcus
Marcus May 9 '20
hi Dave thanks for reply. its actually third party library bro i was taking about. anyways i just dumped all files in class folder renamed main class and a few instances of it. that all it took, works like charm. i really appreciate it buddy like last piece of code you posted gave me a idea.


The Forum post is edited by Marcus May 9 '20
dave Leader
dave May 9 '20
great glad i could help.... :)  and glad its working for you.