Roots Discourse

Custom PHP classes - where and how

sage9

#1

Hello.

I’m implementing a WP page but will have to use certain custom algorithms for a certain form-related data operations. Custom classes would be best for this so I can have it nicely OOP structured, but am not sure where to put them here and how to properly import / manage /test them.

My page is a one-pager, so most things could be imported on the FrontPage.

I managed to make it work by just putting my classes inside app/Controllers with the same existing namespace namespace App\Controllers; and that works, but I don’t think it’s even nice to have them inside controllers, since controllers are directly connected to they overlying views. So would prefer to have my own folder for my “algorithm” and import that on FrontPage or somewhere for example.

Probably even having them inside app/Controllers/algorithm/User.php etc wouldn’t be preferable? What about app/algorithm/User.php etc. ?

So where would be the correct place to use our custom classes in the Sage 9 arhitecture and how would I go about including them properly? Say I have 4 classes: User, Relative, Condition and an Algorithm (and more…) and want to use them all in FrontPage controller for example? And I want to see the errors etc. from all the classes, no matter where they were called from.

Best regards and thanks for any suggestions/best practices :slight_smile:


#2

I would make mu-plugins or proper plugins and throw your classes in there. Then just USE them to display stuff in the theme. The theme is the view layer.


#3

Heya @austin yea that sounds reasonable I guess, better logic/view separation.
You have a simple example of how to USE the plugin classes in one of the Sage files, such as custom ajax.php inside /app/ajax.php (same location as helpers.php for example) ? If so, would love to see it!

thanks!