Do you *truly* use OOP?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 6177 Views - Last Post: 22 March 2010 - 10:07 PM

#1 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Do you *truly* use OOP?

Post icon  Posted 20 October 2009 - 02:07 PM

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):
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..
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?

Is This A Good Question/Topic? 0
  • +

Replies To: Do you *truly* use OOP?

#2 ludjer  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 186
  • Joined: 31-October 08

Re: Do you *truly* use OOP?

Posted 20 October 2009 - 02:58 PM

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
$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
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 October 2009 - 02:59 PM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,752
  • Joined: 16-October 07

Re: Do you *truly* use OOP?

Posted 20 October 2009 - 03:17 PM

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.
Was This Post Helpful? 1
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Do you *truly* use OOP?

Posted 20 October 2009 - 03:25 PM

@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.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,752
  • Joined: 16-October 07

Re: Do you *truly* use OOP?

Posted 20 October 2009 - 03:56 PM

View PostRudiVisser, on 20 Oct, 2009 - 04:25 PM, said:

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.

View PostRudiVisser, on 20 Oct, 2009 - 04:25 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Do you *truly* use OOP?

Posted 20 October 2009 - 04:20 PM

View Postbaavgai, on 20 Oct, 2009 - 02:56 PM, said:

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.

View Postbaavgai, on 20 Oct, 2009 - 02:56 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#7 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: Do you *truly* use OOP?

Posted 20 October 2009 - 04:23 PM

Quote

<?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

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.
Was This Post Helpful? 0
  • +
  • -

#8 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Do you *truly* use OOP?

Posted 20 October 2009 - 04:37 PM

View PostMaster Jake, on 20 Oct, 2009 - 03:23 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#9 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Do you *truly* use OOP?

Posted 20 October 2009 - 04:41 PM

View PostMaster Jake, on 20 Oct, 2009 - 04:23 PM, said:

Quote

<?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

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:
$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~
Was This Post Helpful? 0
  • +
  • -

#10 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: Do you *truly* use OOP?

Posted 20 October 2009 - 04:41 PM

View PostRudiVisser, on 20 Oct, 2009 - 03:37 PM, said:

View PostMaster Jake, on 20 Oct, 2009 - 03:23 PM, said:

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 :)
Was This Post Helpful? 0
  • +
  • -

#11 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Do you *truly* use OOP?

Posted 20 October 2009 - 04:44 PM

View PostMaster Jake, on 20 Oct, 2009 - 03:41 PM, said:

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 :)

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..

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

vs
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 October 2009 - 04:48 PM

Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3028
  • View blog
  • Posts: 10,547
  • Joined: 08-August 08

Re: Do you *truly* use OOP?

Posted 21 October 2009 - 05: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.
Was This Post Helpful? 0
  • +
  • -

#13 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Do you *truly* use OOP?

Posted 21 October 2009 - 05:39 AM

View PostCTphpnwb, on 21 Oct, 2009 - 04:34 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3028
  • View blog
  • Posts: 10,547
  • Joined: 08-August 08

Re: Do you *truly* use OOP?

Posted 21 October 2009 - 05:52 AM

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 October 2009 - 05:52 AM

Was This Post Helpful? 0
  • +
  • -

#15 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Do you *truly* use OOP?

Posted 23 October 2009 - 07:51 AM

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?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3