School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 306,725 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,583 people online right now. Registration is fast and FREE... Join Now!




Do you *truly* use OOP?

2 Pages V  1 2 >  

Do you *truly* use OOP?

RudiVisser

20 Oct, 2009 - 01:07 PM
Post #1

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,890



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
PHP is only an "implied-OOP" (self-coined term) language, which means you can very easily slip out of using OOP when you're programming a web application.

The question I want to ask you all is do you treat programming PHP applications as you were programming in a "forced-OOP" language. What I'm basically getting at is, in compaison to C#, do you write your code the same?? In C#, other .NET languages and other "forced-OOP" languages, everything is an object, contained within a class (and depending on the language, contained within a namespace - But I won't go into that).

In PHP, it's more than common practice to create a, for example, Database layer class. Such class would provide methods such as ->Query, ->Row_Count, ->Get_Records, along with several other common database functions. If you was to be writing this class, what would you return from them?? The most common way would be to do what your instinct says, due to the very loosely-typed nature of PHP, and return an array, or a raw resultset, depending on the method.

Example (note this is PURELY for demonstration + just written and may not work/be accurate):
CODE
public function Get_Records($Query) {
    $RawResults = $this->Query($Query);
    $Return = array();
    while($RawRecord = mysql_fetch_assoc($RawResults)) {
        $Return[] = $RawRecord;
    }
    return $Return;
}


The second option would be to do it the true OOP way, which would be to create a class that represented either a single result or a set of results, along with associated methods to retrieve the data in different ways, and manipulate the data.

Example (again, PURELY for demonstration + just written and may not work/be accurate):
This is more pseudo than practical, as some methods require more data..
CODE
class Record {
    private $Raw;

    // Let's say, just for example, that this class is constructed with a mysql_fetch_assoc() of a row as the first argument, which could be passed/created from a Records class.
    public __construct($_Raw) {
        $this->Raw = $_Raw;
    }

    public function Get_Field($name) {
        if(!isset($this->Raw[$name])) return "Not Available";
        return $this->Raw[$name];
    }

    public function Update_Field($name, $value) {
        if(!isset($this->Raw[$name])) return false;
        $this->Raw[$name] = $value;
        // Run a DB query to update this exact record..
    }

    // etc..

}


Obviously the above is only practical in certain circumstances, probably not when just retrieving a single record, however imagine mapping a whole table. Table class containing a Records class which contained several Record instances.. Data about the table's primary keys would be stored to allow for more reliable updating, etc. etc.

Anyway enough "examples" and more asking - Do you use OOP fully?

User is offlineProfile CardPM
+Quote Post


ludjer

RE: Do You *truly* Use OOP?

20 Oct, 2009 - 01:58 PM
Post #2

D.I.C Head
**

Joined: 31 Oct, 2008
Posts: 108



Thanked: 2 times
My Contributions
well i use mainly oop to access my sql database, and also to do the templates but i dont think its full oop is a half half assed setup i think though i am interested in using it fully since only good could come out of it.
this is how i use oop to get database stuff
php

<?PHP
$users = $db->get_results("SELECT name, email FROM users");
foreach ( $ users as $user )
{
// Access data using object syntax
echo $user->name;
echo $user->email;
}
?>

im using a class called ez_sql to do this, very useful and easy to use as the original author said(PHP and Working with Databases (for the Lazy Sod))

when i start a application i try to always use classes cause i think it allows my application to become more flexible and more structured but i dont follow it to the extend i should.

php

<?PHP
include "Classes/session.php";
include "templategen.php";
include "Classes/ez_sql.php";
include "Classes/acl.php";
$template = new template;
$template->template("This is the title");
$template->add_notice("Under Construction","notice");
$template->add_head("Headder");
$template->add_par("Lorem ipsum ...");
$template->add_head("Headder2");
$template->add_par("Quisque a mauris...");
$template->generate_content();
$template->generate();
?>

that is how i do the more static pages
with the ones that require a bit more dynamic user generated content ill call there class ie $reports = new reports;
and then ill do all the required stuff and return $content and then ill $template->add_content($content);
thats how i use oop in my code

This post has been edited by ludjer: 20 Oct, 2009 - 01:59 PM
User is offlineProfile CardPM
+Quote Post

baavgai

RE: Do You *truly* Use OOP?

20 Oct, 2009 - 02:17 PM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,346



Thanked: 410 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
I use OOP in OOP based languages. PHP object handling is rudimentary at best. Because of a number of design considerations, using object design concepts in PHP might actually be detrimental.

Think about how a web page works. It loads what it needs to run, does it's job, and ends, all in response to a single request. In a modern system, that functionality is augmented some intelligent caching. However, the process is still kludgey. And the more files you load, the uglier it gets.

Web pages intrinsically lend themselves to a model-view-controller design. The page is the view, the controller is the shared libraries, the model hides behind that controller. The more you can break that controller code up into what's only required for a particular page and what needs to be shared, the better your design.

Object oriented design is about code reuse. If you have to load ten class definition to respond to a user's request for a single object, that's fine. On a client's single persistent session memory model, it's not even an issue. It's often not that useful for web pages.

In middle tier languages, like Java or .NET, all these objects essentially run in a single shared environment. In PHP, not so much.

User is online!Profile CardPM
+Quote Post

RudiVisser

RE: Do You *truly* Use OOP?

20 Oct, 2009 - 02:25 PM
Post #4

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,890



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
@baavgai Indeed, there's also no state which means using OOP on a large scale when having to reconstruct a state (user logged in, etc.) could definitely be detrimental.

However on a large scale website (think something like eBay, Amazon, etc. etc.) it would definitely be a good idea to use complete OOP with PHP, replacing a single object is easier than replacing a codefile that replaces several related (and possibly unrelated) functions to acheive a task, if everything's self contained and I'm making sense you'll see what I mean.
User is offlineProfile CardPM
+Quote Post

baavgai

RE: Do You *truly* Use OOP?

20 Oct, 2009 - 02:56 PM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,346



Thanked: 410 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(RudiVisser @ 20 Oct, 2009 - 04:25 PM) *

However on a large scale website (think something like eBay, Amazon, etc. etc.) it would definitely be a good idea to use complete OOP


Agreed.

QUOTE(RudiVisser @ 20 Oct, 2009 - 04:25 PM) *

with PHP


Strangely, not done. The big guys use huge server farms and an intelligent middle tier. Amazon, at least, is Java heavy. You'll find that the big guys that use a PHP use it as site glue and will often have something else doing heavy lifting behind the facade.

The reliable, large scale, "pure" PHP sites lean toward PHP 4 and usually eschew OOP. They often offset scaling limitations by using something like an Oracle database.

User is online!Profile CardPM
+Quote Post

RudiVisser

RE: Do You *truly* Use OOP?

20 Oct, 2009 - 03:20 PM
Post #6

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,890



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
QUOTE(baavgai @ 20 Oct, 2009 - 02:56 PM) *

Strangely, not done.

Indeed, but until later versions of PHP 5.2(+) OOP in PHP wasn't really a "nice" experience. With the addition of namespaces I'm really thinking that this could be adopted more, especially in large sites like I say.

QUOTE(baavgai @ 20 Oct, 2009 - 02:56 PM) *

The reliable, large scale, "pure" PHP sites lean toward PHP 4 and usually eschew OOP. They often offset scaling limitations by using something like an Oracle database.

Same as above really.. I haven't seen a MySQL Enterprise Cluster perform bad yet, with a load balanced farm and maybe a central core (not run on PHP?), I'm confident in PHP/MySQL's ability to scale... Don't think I'm *too* alone in this thought.
User is offlineProfile CardPM
+Quote Post

Master Jake

RE: Do You *truly* Use OOP?

20 Oct, 2009 - 03:23 PM
Post #7

D.I.C Head
Group Icon

Joined: 27 Feb, 2009
Posts: 106



Thanked: 6 times
Dream Kudos: 150
My Contributions
QUOTE

php

<?PHP
$users = $db->get_results("SELECT name, email FROM users");
foreach ( $ users as $user )
{
// Access data using object syntax
echo $user->name;
echo $user->email;
}
?>



Not to go off topic here, but isn't foreach a bad method of looping arrays? I know it's designed especially for arrays, but I remember reading on a few occasions about foreach being a memory hog and a slow way to loop arrays. I much prefer while in use such as:

php

<?php

while ($users = $db->get_results("SELECT name, email FROM users"))
{
// Access data using object syntax
echo $users->name;
echo $users->email;
}

?>


So as I'm not completely off topic (and to answer the original question), I do not use OOP in PHP. I rarely use it period as I am procedural oriented programming more, these days, as opposed to object oriented programming.
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Do You *truly* Use OOP?

20 Oct, 2009 - 03:37 PM
Post #8

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,890



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
QUOTE(Master Jake @ 20 Oct, 2009 - 03:23 PM) *

Not to go off topic here, but isn't foreach a bad method of looping arrays? I know it's designed especially for arrays, but I remember reading on a few occasions about foreach being a memory hog and a slow way to loop arrays. I much prefer while in use such as:

??

Don't know where you read that, seems as silly as the echo vs print argument to me, especially nowadays.

Although it's not ideal from a programming perspective, fact is, servers these days have more than enough memory to handle even the most demanding of scripts. Even on a server with low-memory, general websites arn't intensive at all (unless something is seriously wrong / it's coded by an idiot), so saving a few KB here and there doesn't matter at all. In the case of PHP, it's also even more irrelevant due to garbage collection/process recycling.
User is offlineProfile CardPM
+Quote Post

ShaneK

RE: Do You *truly* Use OOP?

20 Oct, 2009 - 03:41 PM
Post #9

require_once("brain.php"); //Fatal error :/
Group Icon

Joined: 10 May, 2009
Posts: 701



Thanked: 47 times
Dream Kudos: 75
Expert In: PHP, MySQL

My Contributions
QUOTE(Master Jake @ 20 Oct, 2009 - 04:23 PM) *

QUOTE

php

<?PHP
$users = $db->get_results("SELECT name, email FROM users");
foreach ( $ users as $user )
{
// Access data using object syntax
echo $user->name;
echo $user->email;
}
?>



Not to go off topic here, but isn't foreach a bad method of looping arrays? I know it's designed especially for arrays, but I remember reading on a few occasions about foreach being a memory hog and a slow way to loop arrays. I much prefer while in use such as:

php

<?php

while ($users = $db->get_results("SELECT name, email FROM users"))
{
// Access data using object syntax
echo $users->name;
echo $users->email;
}

?>


So as I'm not completely off topic (and to answer the original question), I do not use OOP in PHP. I rarely use it period as I am procedural oriented programming more, these days, as opposed to object oriented programming.


That's not looping an array. get_results, in that case, is a function that returns false when there's nothing else to get, looping an array with a while loop you'd have to do something like:
CODE
$i = 0;
while(count($some_array) != $i){
    $variable = $some_array[$i];
    //Do this
    $i++;
}


And I honestly can't see that being much better.

Yours,
Shane~
User is offlineProfile CardPM
+Quote Post

Master Jake

RE: Do You *truly* Use OOP?

20 Oct, 2009 - 03:41 PM
Post #10

D.I.C Head
Group Icon

Joined: 27 Feb, 2009
Posts: 106



Thanked: 6 times
Dream Kudos: 150
My Contributions
QUOTE(RudiVisser @ 20 Oct, 2009 - 03:37 PM) *

QUOTE(Master Jake @ 20 Oct, 2009 - 03:23 PM) *

Not to go off topic here, but isn't foreach a bad method of looping arrays? I know it's designed especially for arrays, but I remember reading on a few occasions about foreach being a memory hog and a slow way to loop arrays. I much prefer while in use such as:

??

Don't know where you read that, seems as silly as the echo vs print argument to me, especially nowadays.

Although it's not ideal from a programming perspective, fact is, servers these days have more than enough memory to handle even the most demanding of scripts. Even on a server with low-memory, general websites arn't intensive at all (unless something is seriously wrong / it's coded by an idiot), so saving a few KB here and there doesn't matter at all. In the case of PHP, it's also even more irrelevant due to garbage collection/process recycling.


I can't remember where I read it at. I just prefer using while. It solves the same thing in less lines of code if used properly smile.gif
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Do You *truly* Use OOP?

20 Oct, 2009 - 03:44 PM
Post #11

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,890



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
QUOTE(Master Jake @ 20 Oct, 2009 - 03:41 PM) *

I can't remember where I read it at. I just prefer using while. It solves the same thing in less lines of code if used properly smile.gif

Well to use while() to properly iterate an array (which it isn't built for), you'd need to call 2 other functions on every iteration, which is obviously much more intensive..

CODE
foreach($Array as $Key => $Val) { /* .. */ }

vs
CODE
while( list($Key, $Val) = each($Array) ) { /* .. */ }


Which is better/easier to understand?

EDIT: Or use an indexer to grab a certain array index like ShaneK said..

This post has been edited by RudiVisser: 20 Oct, 2009 - 03:48 PM
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: Do You *truly* Use OOP?

21 Oct, 2009 - 04:34 AM
Post #12

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,101



Thanked: 155 times
Dream Kudos: 100
Expert In: PHP

My Contributions
The way I see it, you've got two choices: OOP and procedural code. I believe that procedural is almost always bad. Because it makes editing another person's work a nightmare if it's beyond a few dozen lines of code it's the reason there are so many confused newbies on this site. There is no structure, and people freely mix html with php, making the code seem like a random sequence of instructions.

Because procedural code cannot scale well and it's difficult to make changes, I think it's the reason PHP has a poor reputation among programmers who use more structured languages. Hopefully, as more sites take up PHP 5 that will change. Unfortunately, procedural coding won't die easily as it's the long time PHP coders that teach the newbies, and they tend to ignore its OOP capabilities.

I'm not saying that everything needs to be OOP though. I try to write everything using class definitions followed by controlling code, which although it calls the class methods, could be considered procedural because all it does is make decisions about what class methods/variables to call. Sometimes I find myself slipping out of that, making the controlling code do more than I intended. That's when I find problems! A little procedural code is ok. More than a little is trouble.

User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Do You *truly* Use OOP?

21 Oct, 2009 - 04:39 AM
Post #13

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,890



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
QUOTE(CTphpnwb @ 21 Oct, 2009 - 04:34 AM) *

The way I see it, you've got two choices: OOP and procedural code. I believe that procedural is almost always bad. Because it makes editing another person's work a nightmare if it's beyond a few dozen lines of code it's the reason there are so many confused newbies on this site. There is no structure, and people freely mix html with php, making the code seem like a random sequence of instructions.

Because procedural code cannot scale well and it's difficult to make changes, I think it's the reason PHP has a poor reputation among programmers who use more structured languages. Hopefully, as more sites take up PHP 5 that will change. Unfortunately, procedural coding won't die easily as it's the long time PHP coders that teach the newbies, and they tend to ignore its OOP capabilities.

I'm not saying that everything needs to be OOP though. I try to write everything using class definitions followed by controlling code, which although it calls the class methods, could be considered procedural because all it does is make decisions about what class methods/variables to call. Sometimes I find myself slipping out of that, making the controlling code do more than I intended. That's when I find problems! A little procedural code is ok. More than a little is trouble.

Yep!

But this is what I'm getting at, I mean the first DB example.. Returning records from a database, would you just return an array of arrays containing the data? Or would you create (a) Record(s) object(s) that provided self-contained methods to manipulate that record/recordset??

I personally have used, in the past, just returning an array, iterated it myself and ran seperate queries to update them.. I want to change from that to be more structured like you say - and that's why I thought it was an interesting topic - because I use OOP, I'm used to OOP, but I still slip out of it for most stuff atm.
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: Do You *truly* Use OOP?

21 Oct, 2009 - 04:52 AM
Post #14

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,101



Thanked: 155 times
Dream Kudos: 100
Expert In: PHP

My Contributions
There's no reason that $object->records can't be an array of arrays. Then it's a matter of passing that information to your display object:
$display->output_data($object->records);

Or I might have the display class extend the model class and just use one object. Either way, it makes for editable code, and that's much more important than negligible performance hits.

This post has been edited by CTphpnwb: 21 Oct, 2009 - 04:52 AM
User is offlineProfile CardPM
+Quote Post

smacdav

RE: Do You *truly* Use OOP?

23 Oct, 2009 - 06:51 AM
Post #15

D.I.C Head
**

Joined: 6 Jun, 2009
Posts: 132



Thanked: 34 times
My Contributions
I just began learning to use PHP in July and I had trouble finding a book in my local bookstore (I realize I could have looked online) or any online tutorials that covered OOP aspects of PHP. (This was before I really discovered DIC, so I had no one to ask then.)

Even though the book I wound up using covers PHP 5.2, it doesn't really mention OOP. As a result, I'm about 3/4 done with a major project and I don't even know how to write a class in PHP. Obviously, I'm writing procedural code, though I do use some objects provided by others (I'm using PEAR DB with mysqli to access my database).

Mentioning mysqli reminds me that the book also uses the mysql extension instead. I had read somewhere that it was better to use mysqli, so I figured out the differences using online documentation. Do you experts have an opinion on which is better: mysql or mysqli?

I'm rambling, sorry. The point is, I found it difficult to find a source that would teach me OOP in PHP. Does anyone have a recommendation for a good source?
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Do You *truly* Use OOP?

23 Oct, 2009 - 06:54 AM
Post #16

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,890



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
QUOTE(smacdav @ 23 Oct, 2009 - 06:51 AM) *

Mentioning mysqli reminds me that the book also uses the mysql extension instead. I had read somewhere that it was better to use mysqli, so I figured out the differences using online documentation. Do you experts have an opinion on which is better: mysql or mysqli?

Well, if you're going to use the OOP Interface (or stuff like prepared statements) with MySQLi, MySQLi smile.gif If not, then there's probably no reason to change.

QUOTE(smacdav @ 23 Oct, 2009 - 06:51 AM) *

I'm rambling, sorry. The point is, I found it difficult to find a source that would teach me OOP in PHP. Does anyone have a recommendation for a good source?

PHP.net! Seriously, it will tell you everything you could ever want to know about classes, what they're useful for and how to use them (effectively).
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: Do You *truly* Use OOP?

23 Oct, 2009 - 07:43 AM
Post #17

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,101



Thanked: 155 times
Dream Kudos: 100
Expert In: PHP

My Contributions
QUOTE(smacdav @ 23 Oct, 2009 - 10:51 AM) *
The point is, I found it difficult to find a source that would teach me OOP in PHP.

This is the biggest problem I see with PHP. It got too well established before getting OOP capabilities and so the majority of coders don't use it, making the language look bad, and keeping new coders from learning it.
crazy.gif
User is offlineProfile CardPM
+Quote Post

AdaHacker

RE: Do You *truly* Use OOP?

23 Oct, 2009 - 03:00 PM
Post #18

D.I.C Regular
***

Joined: 17 Jun, 2008
Posts: 394



Thanked: 86 times
My Contributions
QUOTE(smacdav @ 23 Oct, 2009 - 08:51 AM) *
Do you experts have an opinion on which is better: mysql or mysqli?

Easy: MySQLi. No contest. It's got the object oriented interface, support for prepared statements, transactions, and the full range of new MySQL features. I mean, the name is short for "MySQL Improved", for crying out loud! How much more of a hint do people need? You simply shouldn't be using the old MySQL extension for new projects and that's all there is to it. Even the PHP manual says so.
User is online!Profile CardPM
+Quote Post

TMKCodes

RE: Do You *truly* Use OOP?

24 Oct, 2009 - 12:17 AM
Post #19

D.I.C Head
Group Icon

Joined: 21 Mar, 2009
Posts: 165



Thanked: 3 times
Dream Kudos: 75
My Contributions
Myself i do not truly use OOP in PHP, even tough i know how to use it, but it's just that i have got used to procedural code before PHP got OOP.

One major thing is that the new people learning PHP does not find about OOP is that they use sites like W3Schools tutorials for learning it and it does not teach about PHP's OOP.

People should use PHP's own documentation to learn it -> http://www.php.net/manual/en/
User is online!Profile CardPM
+Quote Post

tivrfoa

RE: Do You *truly* Use OOP?

24 Oct, 2009 - 09:31 AM
Post #20

D.I.C Head
Group Icon

Joined: 25 Jan, 2009
Posts: 92



Thanked: 6 times
Dream Kudos: 125
My Contributions
QUOTE(Master Jake @ 20 Oct, 2009 - 03:23 PM) *

Not to go off topic here, but isn't foreach a bad method of looping arrays? I know it's designed especially for arrays, but I remember reading on a few occasions about foreach being a memory hog and a slow way to loop arrays. I much prefer while in use such as:

php

<?php

while ($users = $db->get_results("SELECT name, email FROM users"))
{
// Access data using object syntax
echo $users->name;
echo $users->email;
}

?>


You are in an infinity loop here (supposing get_results return something), and it will print always the same thing...

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 02:08PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month