Skip to main content
Topic: OO PHP - Need help (Read 3499 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

OO PHP - Need help

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

Re: OO PHP - Need help

Reply #1

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
Last Edit: October 03, 2014, 12:09:04 pm by emanuele
Bugs creator.
Features destroyer.
Template killer.

Re: OO PHP - Need help

Reply #2

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

Re: OO PHP - Need help

Reply #3

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.

Re: OO PHP - Need help

Reply #4

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 I was playing with :-[ .

Re: OO PHP - Need help

Reply #5

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() which should be used. Or better would be to put that in a separate JS file and use the vars that are available 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.

Re: OO PHP - Need help

Reply #6

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.

Re: OO PHP - Need help

Reply #7

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


Quote from: groundup – There's a function called addInlineJavascript() which should be used. Or better would be to put that in a separate JS file and use the vars that are available 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.
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

Re: OO PHP - Need help

Reply #8

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

Re: OO PHP - Need help

Reply #9

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.