Can some one help a noob by telling me where the $CFG and $SESSION objects are instantiated and also the dir and file name where those classes are located.
Thanks
In reply to Eugene Ritter
Re: Where is the $CFG object and $SESSION object instantiated
由Gustav W Delius發表於
Most of it is done in lib/setup.php although some of the $CFG properties are already set in config.php (in PHP objects don't have to be explicitly instantiated). The file config.php is included in all Moodle pages and it loads lib/setup.php which in turn loads the three main libraries lib/moodlelib.php, lib/datalib.php and lib/weblib.php.
In reply to Gustav W Delius
Re: Where is the $CFG object and $SESSION object instantiated
由Eugene Ritter發表於
Okay I guess what I'm looking for is a class... Where are the classes for $CFG and $SESSION... Are they not instantiated using $foo = new $foobar; ... ????
I've never seen objects in PHP used in any other way.
What did you mean by ...
"in PHP objects don't have to be explicitly instantiated"
Is there another way to use them?
Thanks
I've never seen objects in PHP used in any other way.
What did you mean by ...
"in PHP objects don't have to be explicitly instantiated"
Is there another way to use them?
Thanks
In reply to Eugene Ritter
Re: Where is the $CFG object and $SESSION object instantiated
由Eugene Ritter發表於
Why would you use an object to do nothing more than carry property values? Why not use an array?
Is it okay to reply to your own posts?
Just so you know about me. I know php and have written and coded with PHP classes. This is the first time I've run into this use of objects before. I'm just trying to understand. I've got a module/module modifications to make and am trying to learn the system.
Thanks
Is it okay to reply to your own posts?

Just so you know about me. I know php and have written and coded with PHP classes. This is the first time I've run into this use of objects before. I'm just trying to understand. I've got a module/module modifications to make and am trying to learn the system.
Thanks
In reply to Eugene Ritter
Re: Where is the $CFG object and $SESSION object instantiated
由Gustav W Delius發表於
Hi Eugene,
I don't think there is a particularly compelling reason why Moodle uses objects a lot in cases where arrays would do the same job. It is just the way Martin liked to do things.
I am not a programmer myself but I know that questions of programming style can lead to heated debates among the enthusiasts. Usually these discussions, like those about taste in general, don't lead anywhere. So I am happy to hear that you are genuinely trying to find out how things are done in Moodle.
I recall from when I first started playing with the Moodle code that looking at setup.php and the three main libraries and then studying the pages in existing modules very quickly gave me a good feel for how things are done around here. After that the code is surprisingly readable. And I assume you have seen the coding guidelines and the development guide?
I don't think there is a particularly compelling reason why Moodle uses objects a lot in cases where arrays would do the same job. It is just the way Martin liked to do things.
I am not a programmer myself but I know that questions of programming style can lead to heated debates among the enthusiasts. Usually these discussions, like those about taste in general, don't lead anywhere. So I am happy to hear that you are genuinely trying to find out how things are done in Moodle.
I recall from when I first started playing with the Moodle code that looking at setup.php and the three main libraries and then studying the pages in existing modules very quickly gave me a good feel for how things are done around here. After that the code is surprisingly readable. And I assume you have seen the coding guidelines and the development guide?
In reply to Gustav W Delius
Re: Where is the $CFG object and $SESSION object instantiated
由Eugene Ritter發表於
I wasn't really looking to debate it. I was just wondering if there was something I didn't know about it that drove the decision. If it's just a style choice than that's fine. I guess I was suprised not to find any classes for some of the main objects like $CFG etc...
I've looked at setup.php and moodle.php and I have got a pretty good start in understanding moodle. I've also looked at the moodle developers guide and coding guidlines. I've also found the sample new module.
Any other tips that might help me?
Thanks
I've looked at setup.php and moodle.php and I have got a pretty good start in understanding moodle. I've also looked at the moodle developers guide and coding guidlines. I've also found the sample new module.
Any other tips that might help me?
Thanks
In reply to Eugene Ritter
Re: Where is the $CFG object and $SESSION object instantiated
由Martín Langhoff發表於
In reply to Martín Langhoff
Re: Where is the $CFG object and $SESSION object instantiated
由Martin Dougiamas發表於
Sure you can use array functions on it, have you tried it? 
It should just work, and at worst you might need to cast it to array.
There are two reasons we use data objects instead of arrays. The first is that array implies order to me, but these objects are not ordered. Secondly, I prefer this cleaner syntax:
$USER->firstname
to this:
$USER['firstname']
It should just work, and at worst you might need to cast it to array.
There are two reasons we use data objects instead of arrays. The first is that array implies order to me, but these objects are not ordered. Secondly, I prefer this cleaner syntax:
$USER->firstname
to this:
$USER['firstname']
In reply to Martin Dougiamas
Re: Where is the $CFG object and $SESSION object instantiated
由Martín Langhoff發表於
I /may/ have done a cast here or there... but I don't think I ever dared commit that code to CVS.
Now that I look (I did a quick grep) there's a bit of casting in setup.php of $CFG to array and back to object again. Interesting!
Also present in auth/ldap/lib.php, but I swear it wasn't me
Now that I look (I did a quick grep) there's a bit of casting in setup.php of $CFG to array and back to object again. Interesting!
Also present in auth/ldap/lib.php, but I swear it wasn't me
In reply to Martín Langhoff
Re: Where is the $CFG object and $SESSION object instantiated
由Petr Skoda發表於
In reply to Martin Dougiamas
Re: Where is the $CFG object and $SESSION object instantiated
由John Papaioannou發表於
Purely academically speaking, arrays don't imply order. Vectors might be a better word for something that implies order. Actually arrays are one of the two concrete implementations of a collection (the other being a linked list), and a collection surely does not imply order.
PHP complicates things here a bit more, because actually all arrays in PHP are hash tables regardless of the variable type of the keys. And hashtables are definitely an unordered collection.
How was that for an anal computer science explanation?
I completely agree that the syntax is cleaner, this is actually a habit that has rubbed off on me during the time I spent hacking Moodle!
The only problem is that those casts "look" free (performance-wise), but you don't actually know what's going on internally.
And also, I haven't seen any documentation on this type of casting in the PHP docs (might be that I haven't searched well enough). This is a bit unsettling, as there's no guarantee that PHP 6 might simply not support casting like that. A tiny tiny bit unsettling.
PHP complicates things here a bit more, because actually all arrays in PHP are hash tables regardless of the variable type of the keys. And hashtables are definitely an unordered collection.
How was that for an anal computer science explanation?

I completely agree that the syntax is cleaner, this is actually a habit that has rubbed off on me during the time I spent hacking Moodle!

And also, I haven't seen any documentation on this type of casting in the PHP docs (might be that I haven't searched well enough). This is a bit unsettling, as there's no guarantee that PHP 6 might simply not support casting like that. A tiny tiny bit unsettling.
