How do I use traits in a moodle local plugin

How do I use traits in a moodle local plugin

by Dulitha Rajapaksha -
Number of replies: 14

I am creating a custom local plugin and I have a plan to put all the common functions into a trait. And I am using php namespaces inside files. Whenever I create a trait and use it inside a classfile in my class folder it gives me a trait not found error. 

The folder structure inside the plugin as below.

  • classes
  • classes/myclass.php
  • db
  • lang
  • traits
  • traits/mytrait.php

Below is how I created my trait.

namespace local_myplugin\traits;

trait mytrait {

public static function abcd() {
echo "HEYY";
}
}

Whenever I use that trait in a class file inside class folder it gives and error saying not found.


Average of ratings: -
In reply to Dulitha Rajapaksha

Re: How do I use traits in a moodle local plugin

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
If the namespace of the trait is local_myplugin\traits, then it would need to go in classes/traits/mytrait.php in order for the automatic class loading to be able to find it (Moodle coding guidelines would suggest that it should be in namespace local_myplugin\local\traits and in classes/local/traits/mytrait.php - as top-level namespaces within plugins are reserved for specific Moodle usage, all custom namespaces should go within local_myplugin\local).
Average of ratings: Useful (1)
In reply to Dulitha Rajapaksha

Re: How do I use traits in a moodle local plugin

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Why?!

Anyway, if you want traits (or interfaces) to be auto-loaded, just put them in the classes folder.

But really, don't use fancy langauges features unless you have a good reason. If you have a bunch of useful methods, just put them in a class called util, or something like that.
Average of ratings: Useful (3)
In reply to Tim Hunt

Re: How do I use traits in a moodle local plugin

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Ah, I use traits to help with the multiple inheritance issue, thus one trait with all my extra methods that is then used when extending the core and core maintenance renderers.  Sort of the same idea / concept as 'interfaces' in Java and '.h' files in C / C++, but clearly with actual implementation rather than just function / method declarations.
In reply to Tim Hunt

Re: How do I use traits in a moodle local plugin

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I've been writing code on and off for 40+ years and - I've tried - but I still think OO is some weird religion smile

Reading about traits in PHP has finally broken my will to live.
Average of ratings: Useful (3)
In reply to Howard Miller

Re: How do I use traits in a moodle local plugin

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Dear Howard,

Think of OO to computing as Tupperware is to foodstuffs, you use it to keep everything in their place and not cross-contaminate each other.

G
Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: How do I use traits in a moodle local plugin

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I didn't say that I didn't understand OO. I understand it enough to think it's mostly a bad idea. You only have to read the "Gang of Four" book to think "no way, it cannot possibly be this difficult". 

However, each to their own. As I always say, you still have to write the code smile
In reply to Howard Miller

Re: How do I use traits in a moodle local plugin

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Ah I see Howard, I've not read the book, however I was fortunate to attend a seminar at a conference with John Vlissides before he passed away.

The think is, I find OO easier than spending days debugging the change to a global variable in obscure functions. In my experience, OO is just as buggy as Functional programming, but the bugs tend to be self contained in their own scope, so once found are easier to deal with as they're all in one area, thus the impact of a fix is also localised.
In reply to Gareth J Barnard

Re: How do I use traits in a moodle local plugin

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
P.S. Do you still need help Dulitha?
In reply to Gareth J Barnard

Re: How do I use traits in a moodle local plugin

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Nope... I give in smile
In reply to Gareth J Barnard

Re: How do I use traits in a moodle local plugin

by Colin Fraser -
Picture of Documentation writers Picture of Testers
Winston Churchill did say that success is going from failure to failure without a loss of enthusiasm..smile
In reply to Colin Fraser

Re: How do I use traits in a moodle local plugin

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
RE: "Winston Churchill did say that success is going from failure to failure without a loss of enthusiasm" - repeated failure without learning from your mistakes sounds like incompetence to me.

Also, I think we're getting topic creep here! So perhaps another more generic post on OO and Moodle, and keep this to traits only.
In reply to Gareth J Barnard

Re: How do I use traits in a moodle local plugin

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I'm not good with cultural references. I should get out more wink
In reply to Gareth J Barnard

Re: How do I use traits in a moodle local plugin

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Global variables are still evil. OO was originally invented to deal with state and that was the big thing... state would be contained in smaller entities. However, you still have to have global state somewhere so that all that happens is that you have some entity containing your "global variables". Back to square one. You can be a crap programmer or a good programmer. OO, functional or procedural programming makes no difference at all to that wink
Average of ratings: Useful (1)