Page 1 of 5

OO concepts

Posted: Sun Feb 01, 2009 3:01 pm
by diG1tY
Hello to all. I found phpDN thru google and I joined up. As I am still a novice here(both in programming and PHP) my question is: is it required to use the OO capabilities? Let's say that you have a small page and you have a big pages. When would it be appropriate to use classes and objects? Is there any advantage of using OO and objects over the 'other' way?

Re: OO concepts

Posted: Sun Feb 01, 2009 3:18 pm
by JAB Creations
Hi diG1tY and welcome to DevNetwork.

Object oriented PHP becomes increasingly desirable as the information you need to have access to becomes more common and complex to access. So in a sense OO counters CC! :mrgreen:

One example is if there is for any insane reason you're executing regex on every single page on your site I would recommend setting a session, executing that regex once, and saving the string to the session and or cookie.

Another example I use is for handling browsers. My site can not correctly be rendered by any browser right now (display: block for inline elements are not rendered with block-level height in all browsers in example) so I have to load a secondary stylesheet to override the first and I have to determine the rendering engine to determine the correct secondary CSS file to load. I'm a purest, what can I say? :twisted:

There are plenty of other examples I'm sure others could give you.

Re: OO concepts

Posted: Sun Feb 01, 2009 3:26 pm
by Eran
Object oriented programming is a methodology for handling complexity in software development. It is certainly not required in PHP, which is procedural in its roots. Object oriented programming is a powerful technique, but is much less intuitive to use than procedural programming - for that reason, I would recommend that you focus on understanding procedural programming first, using basic control structures and simple functions to manage your code.

When you feel that your applications are outgrowing your ability to manage their complexity, that would be a good time to start learning object oriented programming.

Re: OO concepts

Posted: Sun Feb 01, 2009 3:34 pm
by diG1tY
What does procedural means? Somethine like interptered language ?

Re: OO concepts

Posted: Sun Feb 01, 2009 3:48 pm
by Eran
Not at all. Procedural programming is the programming style you are probably most familiar with - in which you specify the steps the program needs to run one after the other, all happening in the same main program scope (aside from functions). In this style data and functions are freely intermixed. Object oriented programming introduces a new scope - the class scope, which allows you to group functions and data together to create more meaningful program structures.

Most object oriented languages use procedural programming to implement the specifics of the application, ie - inside class methods. Object oriented programming simplified is one more language structure for use in procedural programming.

Re: OO concepts

Posted: Sun Feb 01, 2009 4:39 pm
by diG1tY
That makes it a little bit more clear now. Even though I don't get all of it. As it seems you don't need OO where they are not needed. If you are given a specific task for it - you go for it. As long as there is no need - in terms of projects, employer or otherwise, there is no need to use the OO methodology. Also does it makes it better or worse? What I mean with the question is that you can have a good coding with and without OO right? As long as the page is working correctly(logically and semantic) it's okay going with or without OO is the same?

Re: OO concepts

Posted: Sun Feb 01, 2009 4:52 pm
by Eran
You can program well with and without OO concepts. OO gives you more tools to manage complexity, but requires more experience to apply effectively. It's a give and take.

Re: OO concepts

Posted: Sun Feb 01, 2009 5:36 pm
by JAB Creations
A good example on my site is all the cookie based user preferences. If you visit my site and start saving preferences there is a cookie with a value like the following...
audio.0_backgroundimages.1_browserpatch.1_chatroom.0_connection.2_css3.1_cursors.1_dhtmleffects.1_dtd.1_ieccss.1_initialfocus.content_keyboardlayout.mediatype.xx_personality.0_powerkeys.1_sounds.1_theme.classic_
My OOP file splits this and assigns each value to an class variable. That way I don't have to constantly determine with the preference is. Imagine having to dictate every instance where I'd have to test for $_GET, then $_COOKIE, then for $_SESSION, etc. I do this once for the class and set a class variable. That way instead of doing all the procedural programming of..

Procedural...

Code: Select all

if (isset($_GET['audio']) {...}
else if (isset($_SESSION)) {...}
else if (isset($_COOKIE['settings'])) {...}
else {...}
...plus all the splitting and so forth for the cookie I do it once and then end up just referring to it as an object. So it's like a super variable in a sense.

You'll know you'll need to use it when you need (in example) a variable that you have to reference over and over but to determine the variable to begin with you have to use a lot of the same code in every single file that you reference it. If you update the code to determine the variable in one file it may break if you update the code in nine files though you had ten copies.

OOP...

Code: Select all

if ($settings->get('audio') == "0") {...}
Also by using OOP in PHP you don't have to have two files directly included by one to another directly. I think it's a sort of line of site...file A has the class set and is included by header.php which is included by the first of several template files...of which the third template file includes a sidebar item of which requires the class set in the original file. I think it works along those lines but pytrin and others are a better versed in the advanced aspects of how it all works.

You may also wonder how a class is different from a function. A function is called once and executes. Usually it returns though I also use them to make direct client output (for proprietary pages now adays only though such as my blog and forum). The issue with a function is that you're still executing procedural programming...so in effect you're still running the code once for every time you call the function so if you have 11 instances where you need that value determined and you call the function 11 times the function has to execute 11 times whereas a PHP OOP class executes once and remains in the memory until the client output is finished. Beyond that there are cookies, sessions, etc. I think that's a half descent way to explain it at least. :)

Another issue I'm quickly finding as I'm making SEO pages is that I am highly considering moving my MySQL queries to be assigned to classes though I'm not certain about it at this point. I should actually start a separate thread as to not hijack yours. Any way I hope we've been helpful. :)

Re: OO concepts

Posted: Sun Feb 01, 2009 5:59 pm
by Apollo
Sorry JAB but I think your examples aren't especially clarifying :)
JAB Creations wrote:One example is if there is for any insane reason you're executing regex on every single page on your site I would recommend setting a session, executing that regex once, and saving the string to the session and or cookie.
What does that have to do with OO vs procedural?
JAB Creations wrote:The issue with a function is that you're still executing procedural programming...so in effect you're still running the code once for every time you call the function so if you have 11 instances where you need that value determined and you call the function 11 times the function has to execute 11 times whereas a PHP OOP class executes once and remains in the memory until the client output is finished.
Uhm.. in the procedural situation, if you have 11 instances of what? If you're calling a function 11 times to calculate a constant result, it's just a matter of caching (e.g. storing the result in a variable) and is not related to OO.

A class does not execute, and if you call a function 11 times, it is executed 11 times. Whether it's a class member or global function does not make any difference at all.

Re: OO concepts

Posted: Sun Feb 01, 2009 6:08 pm
by Apollo
diG1tY wrote:As it seems you don't need OO where they are not needed.
OO is never 'needed'. You may benefit from it if applied wisely. Using an OO approach is more suitable in some situations than others, but it's never 'required'.
Also does it makes it better or worse? What I mean with the question is that you can have a good coding with and without OO right? As long as the page is working correctly(logically and semantic) it's okay going with or without OO is the same?
OO does not by itself make things better or worse. OO is not some magic element you can add to your code which suddenly makes everything streamlined and organized. It's just a different approach to organizing your code.

Think of it somewhat like this: if you're doing some project in PHP, and you're writing a lot of functions, you can put different groups of functions in different php files, and include them from the main file. You can also keep all functions and everything in one big php file. Now, which one is better?

Re: OO concepts

Posted: Mon Feb 02, 2009 6:18 am
by diG1tY
I am not quite sure, I guess it depends from the programmer. If they both work correctly and are 'liked', it seems it doesn't matter?

Re: OO concepts

Posted: Mon Feb 02, 2009 6:39 am
by Chris Corbyn
To answer Apollo's question, putting the functions into separate files is better.

Why? Because when you write software (and web applications) one of your concerns must be about how easy the code is going to be to maintain and fix bugs in as time goes on. Could you imagine what 20,000 lines of code in one file would be like to edit? :)

No matter whether you're using procedural code (i.e. non-OOP) or OOP you still have to apply some basic principles to keep your code clean and easy to work with.

As pytrin says, learn the basics of PHP first and once you start to feel like your code is becoming trickier to adapt to do different things that would be a good time to take a look at OOP. It's hard to see the benefits of something before you feel that you need it (or realise why you need it).

That said, there are some well written applications using procedural code.

C, the language in which PHP itself is written is a completely procedural language.

Re: OO concepts

Posted: Mon Feb 02, 2009 7:02 am
by papa
Good practise is to write an application as you would normally do, and then rewrite that application with OOP.

When I first started programming and didn't know OOP, I had just heard about it and knew it was something that I could have a lot of use for. It took a couple of applications and practise before I really wrote OOP that was useful and not only using the syntax for it.

piz

Re: OO concepts

Posted: Mon Feb 02, 2009 9:29 am
by josh
IMO you should write OO from the start, whether or not your employer requires it. Unless your application needs to run on an embedded system or a language where objects just aren't practical, there is no reason not to use it. Learn about it and see for yourself.

Re: OO concepts

Posted: Mon Feb 02, 2009 11:27 am
by Christopher
jshpro2 wrote:IMO you should write OO from the start, whether or not your employer requires it. Unless your application needs to run on an embedded system or a language where objects just aren't practical, there is no reason not to use it. Learn about it and see for yourself.
I agree with this -- learn it, use it.
Apollo wrote:OO is never 'needed'. You may benefit from it if applied wisely. Using an OO approach is more suitable in some situations than others, but it's never 'required'.
This is sort of like saying that loops are never 'needed' because you can use an if() and goto to do the same thing. I think you misrepresent the issue.
Apollo wrote:OO does not by itself make things better or worse. OO is not some magic element you can add to your code which suddenly makes everything streamlined and organized. It's just a different approach to organizing your code.
Actually, while not magical, the class construct and related functionality (plus the design and testing benefits, patterns, etc.) are all things that can and do make your code more streamlined and organized. Like anything it can be used poorly, but to say OO does not make the practice of programming better again misrepresents the issue.
Apollo wrote:Think of it somewhat like this: if you're doing some project in PHP, and you're writing a lot of functions, you can put different groups of functions in different php files, and include them from the main file. You can also keep all functions and everything in one big php file. Now, which one is better?
I don't think this example represents a real design difference. If you had compared a function library with associated global variables to a class with properties -- that might have exemplified some of the basic differences.