ElkArte Community

General => Chit Chat => Topic started by: Joker™ on October 03, 2014, 10:39:56 am

Title: OO PHP - Need help
Post by: Joker™ on October 03, 2014, 10:39:56 am
Hi everyone,

At last I've decided to stop writing procedural PHP code and move to OO PHP coding. As it seems I'm pretty late to OO PHP party, so can someone provide me few links to get started with.

For now I'm following these few links to see how things works in oo
http://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762
http://stackoverflow.com/a/20681770
Title: Re: OO PHP - Need help
Post by: emanuele on October 03, 2014, 11:40:19 am
Welcome to the latecomers party! :dancers:

I tend to prefer the "learn-by-doing" approach, so I have not read much...
But, in the last couple of days I was trying to get a hold of dependency injection and all the buzz and I found this blog:
http://misko.hevery.com/2008/10/21/dependency-injection-myth-reference-passing/
It speaks Java, but is not that different LOL
There are some interesting articles I think, at least I think I got a better idea of what is going on. :P
Title: Re: OO PHP - Need help
Post by: Joker™ on October 03, 2014, 12:07:00 pm
Quote from: emanuele – Welcome to the latecomers party! :dancers:
Thanks :D

Quote from: emanuele – I tend to prefer the "learn-by-diong" approach, so I have not read much...
Well I also try to take the same approach for most of the instances, but this time I'm thinking of making an exception by doing some sort of crash course


Quote from: emanuele – But, in the last couple of days I was trying to get a hold of dependency injection and all the buzz and I found this blog:
http://misko.hevery.com/2008/10/21/dependency-injection-myth-reference-passing/
It speaks Java, but is not that different LOL
There are some interesting articles I think, at least I think I got a better idea of what is going on. :P
Surely going to read it out
Title: Re: OO PHP - Need help
Post by: Joshua Dickerson on October 03, 2014, 01:43:17 pm
Hmm... not sure about tutorials. I would look at frameworks like Symfony or ZF. You'll learn a lot about separation of concerns, dependency injection, etc. Creating a good, clean class is an art. Although thinking of things as objects is the basis for OOP, writing good code is how you keep from just making it procedural with namespaces.
Title: Re: OO PHP - Need help
Post by: Joker™ on October 03, 2014, 02:17:09 pm
Well I've started on OOP, and it seems to be a big task for sure, but this time I'm going to nail it. For some reason 'spl_autoload_register' is giving me all kinds of odd className, but not the ones I require.

Joker is going for a small nap (12hrs atleast :P), here's the code (https://github.com/siddhartha-gupta/SMF-Likes/tree/v_2.0) I was playing with :-[ .
Title: Re: OO PHP - Need help
Post by: Joshua Dickerson on October 03, 2014, 04:03:11 pm
Use a PSR4 compliant autoloader: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md even the one from the example and it will be a lot easier.

It is almost always best to not use static. You aren't creating a singleton. As it is now, LikePosts won't work. You need to initialize the class as an object. Instead of using globals, pass them in to the object/method.

There's a function called
addInlineJavascript() (https://github.com/elkarte/Elkarte/blob/master/sources/Load.php#L2198) which should be used. Or better would be to put that in a separate JS file and use the vars that are available (https://github.com/elkarte/Elkarte/blob/master/sources/Load.php#L1633)on every page.

There's no need for the json_encode/decode checks. If I'm not mistaken, Elkarte has a minimum PHP version greater than 5.2 already. If not, that should be a separate file anyway.

Side note: your README.md should be in Markdown, not BBC.
Title: Re: OO PHP - Need help
Post by: Joshua Dickerson on October 03, 2014, 04:07:11 pm
As soon as I clicked post I realized why you are using static functions. Yuck, that's a huge issue with the event system. Sorry, looks like you need to do that for now.
Title: Re: OO PHP - Need help
Post by: Joker™ on October 04, 2014, 02:30:57 am
Quote from: groundup – Use a PSR4 compliant autoloader: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md even the one from the example and it will be a lot easier.
Reading it.. seems interesting


Quote from: groundup – It is almost always best to not use static. You aren't creating a singleton. As it is now, LikePosts won't work. You need to initialize the class as an object. Instead of using globals, pass them in to the object/method.
Okzz, singleton seems promising, working on globals as well


Actually its one of my SMF's mod on which I'm playing/trying out OOP things. As Elk is already in object oriented mode, so I thought of learning on SMF2, as there is nearly zero support for OOP as of now. With Elk I've seen/learnt few things already about OOP.

QuoteSide note: your README.md should be in Markdown, not BBC.
Its readme copy past, joker is lazyyyyyyy :D

Thanks for all the advices and suggestion, back to codes
Title: Re: OO PHP - Need help
Post by: Joker™ on October 06, 2014, 01:07:36 pm
Quote from: groundup – It is almost always best to not use static. You aren't creating a singleton. As it is now, LikePosts won't work. You need to initialize the class as an object. Instead of using globals, pass them in to the object/method.
@groundup, I tried passing global vars in 'LikePosts::getInstance' by reference, but '$context' etc doesn't seem to have update value. I mean, these global vars are set up pretty early in class, but '$context, $settings' seems to be filled up at a later stage. Due to which when the code  tries to access the values from these vars/arrays it fails.

Can you provide me a small snippet about passing the global vars to a class instance, and class can have the update value of the variable
Title: Re: OO PHP - Need help
Post by: Joshua Dickerson on October 06, 2014, 04:19:46 pm
The way the hook system is now, you have to keep it the way you have it. You can't instantiate an object. So, you have to use static methods. Singleton is an anti-pattern which should be avoided.