How do I use traits in a moodle local plugin

How do I use traits in a moodle local plugin

ໂດຍ Dulitha Rajapaksha -
ຈຳນວນການຕອບກັບ: 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.


ການຈັດອັນດັບສະເລ່ຍ: -
ໃນການຕອບກັບຫາ Dulitha Rajapaksha

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

ໂດຍ Davo Smith -
ຮູບພາບຂອງ Core developers ຮູບພາບຂອງ Particularly helpful Moodlers ຮູບພາບຂອງ Peer reviewers ຮູບພາບຂອງ 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).
ການຈັດອັນດັບສະເລ່ຍ:Useful (1)
ໃນການຕອບກັບຫາ Dulitha Rajapaksha

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

ໂດຍ Tim Hunt -
ຮູບພາບຂອງ Core developers ຮູບພາບຂອງ Documentation writers ຮູບພາບຂອງ Particularly helpful Moodlers ຮູບພາບຂອງ Peer reviewers ຮູບພາບຂອງ 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.
ການຈັດອັນດັບສະເລ່ຍ:Useful (3)
ໃນການຕອບກັບຫາ Tim Hunt

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

ໂດຍ Gareth J Barnard -
ຮູບພາບຂອງ Core developers ຮູບພາບຂອງ Particularly helpful Moodlers ຮູບພາບຂອງ 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.
ການຈັດອັນດັບສະເລ່ຍ: -
ໃນການຕອບກັບຫາ Tim Hunt

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

ໂດຍ Howard Miller -
ຮູບພາບຂອງ Core developers ຮູບພາບຂອງ Documentation writers ຮູບພາບຂອງ Particularly helpful Moodlers ຮູບພາບຂອງ Peer reviewers ຮູບພາບຂອງ 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 ຍິ້ມ

Reading about traits in PHP has finally broken my will to live.
ການຈັດອັນດັບສະເລ່ຍ:Useful (3)
ໃນການຕອບກັບຫາ Howard Miller

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

ໂດຍ Gareth J Barnard -
ຮູບພາບຂອງ Core developers ຮູບພາບຂອງ Particularly helpful Moodlers ຮູບພາບຂອງ 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
ການຈັດອັນດັບສະເລ່ຍ:Useful (1)
ໃນການຕອບກັບຫາ Gareth J Barnard

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

ໂດຍ Howard Miller -
ຮູບພາບຂອງ Core developers ຮູບພາບຂອງ Documentation writers ຮູບພາບຂອງ Particularly helpful Moodlers ຮູບພາບຂອງ Peer reviewers ຮູບພາບຂອງ 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 ຍິ້ມ
ການຈັດອັນດັບສະເລ່ຍ: -
ໃນການຕອບກັບຫາ Howard Miller

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

ໂດຍ Gareth J Barnard -
ຮູບພາບຂອງ Core developers ຮູບພາບຂອງ Particularly helpful Moodlers ຮູບພາບຂອງ 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.
ການຈັດອັນດັບສະເລ່ຍ: -
ໃນການຕອບກັບຫາ Gareth J Barnard

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

ໂດຍ Gareth J Barnard -
ຮູບພາບຂອງ Core developers ຮູບພາບຂອງ Particularly helpful Moodlers ຮູບພາບຂອງ Plugin developers
P.S. Do you still need help Dulitha?
ການຈັດອັນດັບສະເລ່ຍ: -
ໃນການຕອບກັບຫາ Gareth J Barnard

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

ໂດຍ Howard Miller -
ຮູບພາບຂອງ Core developers ຮູບພາບຂອງ Documentation writers ຮູບພາບຂອງ Particularly helpful Moodlers ຮູບພາບຂອງ Peer reviewers ຮູບພາບຂອງ Plugin developers
Nope... I give in ຍິ້ມ
ການຈັດອັນດັບສະເລ່ຍ: -
ໃນການຕອບກັບຫາ Howard Miller

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

ໂດຍ Gareth J Barnard -
ຮູບພາບຂອງ Core developers ຮູບພາບຂອງ Particularly helpful Moodlers ຮູບພາບຂອງ Plugin developers
ໃນການຕອບກັບຫາ Gareth J Barnard

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

ໂດຍ Colin Fraser -
ຮູບພາບຂອງ Documentation writers ຮູບພາບຂອງ Particularly helpful Moodlers ຮູບພາບຂອງ Testers
Winston Churchill did say that success is going from failure to failure without a loss of enthusiasm..ຍິ້ມ
ການຈັດອັນດັບສະເລ່ຍ: -
ໃນການຕອບກັບຫາ Colin Fraser

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

ໂດຍ Gareth J Barnard -
ຮູບພາບຂອງ Core developers ຮູບພາບຂອງ Particularly helpful Moodlers ຮູບພາບຂອງ 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.
ການຈັດອັນດັບສະເລ່ຍ: -
ໃນການຕອບກັບຫາ Gareth J Barnard

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

ໂດຍ Howard Miller -
ຮູບພາບຂອງ Core developers ຮູບພາບຂອງ Documentation writers ຮູບພາບຂອງ Particularly helpful Moodlers ຮູບພາບຂອງ Peer reviewers ຮູບພາບຂອງ 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 ກະພິບຕາ
ການຈັດອັນດັບສະເລ່ຍ:Useful (1)