The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.1.2-1ubuntu2.14 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/printthread.php 16 require_once
Warning [2] Undefined property: MyLanguage::$archive_pages - Line: 2 - File: printthread.php(287) : eval()'d code PHP 8.1.2-1ubuntu2.14 (Linux)
File Line Function
/printthread.php(287) : eval()'d code 2 errorHandler->error
/printthread.php 287 eval
/printthread.php 117 printthread_multipage



UserSpice
Autoload Classes - Printable Version

+- UserSpice (https://userspice.com/forums)
+-- Forum: Miscellaneous (https://userspice.com/forums/forumdisplay.php?fid=28)
+--- Forum: Modifications and Hackery (https://userspice.com/forums/forumdisplay.php?fid=29)
+--- Thread: Autoload Classes (/showthread.php?tid=634)

Pages: 1 2


Autoload Classes - firestorm - 07-07-2017

I've implemented autoloading classes, drop your class file into classes folder and away you go, see gist below which has a readme.txt and class.autoloader.php, just put the class file into classes folder, open init and remove all classes called with require_once() and in their place require the class.autoloader.php file:
Code:
require_once ABS_TR_ROOT.TR_URL_ROOT.'users/classes/class.autoloader.php';

thats it, classes will be required only when they are needed, we have only one require rather than multiple reducing overhead.

try it out by placing
Code:
bold("<br><br> Hoorah my class is included !");
in the input class then visit a page with input fields, you'll see "Hoorah my class is included !" only where the input class is used.

https://gist.github.com/Firestorm-Graphics/98f80b2b45c591d107c35c5f7e5c8632

NOTE: chunk6.php is only for userspice installation files


Autoload Classes - mudmin - 07-09-2017

This is cool. I will check it out. We had autoload when we used composer but when I took away composer, some of the other autoloading techniques gave people fits. I'll give this one a whirl.


Autoload Classes - firestorm - 07-09-2017

so far its autoloading userspice classes fine, not ran into any problems so far, im also working on a hooks & filters class ( it works exactly like wordpress's hooks & filters )

so we can register a hook to place in the header for example:
<pre>
Code:
function my_header {
    do_action('my_header');
}
</pre>


then we can create our header function to output meta tags for example:

<pre>
Code:
function my_meta {
  echo 'my meta will be here';
}
add_action('my_meta', 'my_header');
</pre>


the beauty of this is we can use filters or remove the action especially usefull in custom functions in usersc folder, we can use it for menu's, headers, footers etc its realy is unlimited


Autoload Classes - mudmin - 07-09-2017

I like it.




Autoload Classes - firestorm - 07-09-2017

paving the way for extentions


Autoload Classes - firestorm - 07-13-2017

After testing I've found we need to have autoload required BEFORE session_start(); all current us classes loaded up fine but the hooks class I'm working on wouldnt load unless before session_start() and i suspect that could happen with classes in the future, so it will be best to NOT include in chunk6.php in stall files but instead require at the top of init.php BEFORE session_start(); i have updated the gist

Code:
require_once 'classes/class.autoloader.php';

will open new thread regarding hooks & filters


Autoload Classes - mudmin - 07-13-2017

So that's one of those things we have to decide how we're going to handle since everyone would have to edit their init files. If some people have auto-loaded classes and some don't, we won't be able to make assumptions going forward that if we drop in a class (which I have a few coming out) that everyone will have them loaded properly.


Autoload Classes - firestorm - 07-13-2017

well thats where its quite cool, the autoload class loads only if not loaded, I've tested the autoload required at the top of init.php along with the classes manual required at the same time and all good not issues,

in an update we can have a patch file that works like your db patch files which will hold this which places the require to the top of init.php:

<pre>
Code:
<?php
//:: This will patch your init.php from v4.2.9 :://
//:: WARNING ! BACKUP YOUR INIT.PHP BEFORE RUNNING THIS PATCH :://

require_once 'init.php';
require_once 'includes/header.php';
require_once 'includes/navigation.php';

$init = 'init.php';
$AutoLoad = '<?php require_once \'classes/class.autoloader.php\';';

$contents = file_get_contents($init);

$line = '<?php';

$contents = str_replace($line, '', $contents);
file_put_contents($init, $contents);

$lines = file($init);
$fopen = fopen($init, "w+");

fwrite( $fopen, $AutoLoad );
foreach( $lines as $line ) {
    fwrite( $fopen, "$line");
}

fclose ($fopen);

bold("<br><br>You're good to go. Delete patch file and bask in the glory.");
?>
</pre>



Autoload Classes - karsen - 07-15-2017

I like this better than my super-simple autoloader I put in place. I'll see about adding this in to my project sometime this week.

I also agree with making a patch file for it; anyone who has modified their init.php should be skilled enough to make the changes listed on Firestorm's git page to add this in without disrupting their init changes (hopefully!).


Autoload Classes - mudmin - 07-15-2017

A lot of these changes will be rolling out into the main project next week for those who don't want to patch manually. Thanks for everyone holding down the fort over the summer!