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
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Autoload Classes
#1
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-Graphi...5f7e5c8632

NOTE: chunk6.php is only for userspice installation files
  Reply
#2
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.
  Reply
#3
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
  Reply
#4
I like it.

  Reply
#5
paving the way for extentions
  Reply
#6
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
  Reply
#7
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.
  Reply
#8
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>
  Reply
#9
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!).
  Reply
#10
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!
  Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)