Jump to content

Why use OOP?


optikalefx

Recommended Posts

Can someone explain to me why I should use OOP instead of procedure based code.

 

Im building a shopping cart and i created and Item object already and using it to hold data for each item.  But this Object just has properties.  I mean what methods could there possibly be for an item.

 

Why would I need a Cart object either?  There will never be more than 1 instance of the cart running.  So what are the advantages to putting all my functions inside of a class?

 

Thanks, and sorry if your offended by this, im trying to go OOP i just don't see why yet.

Link to comment
Share on other sites

lol, basically a search of wikipedia would help you understand why, php implements it very well as long as you know how to code with classes etc.

 

OOP is not about coding with classes, so maybe you should have a look at some WP articles as well :)

 

As a matter of fact, one might design an object oriented language without the concept of classes.

Link to comment
Share on other sites

Your member group leads me to think that I shouldn't argue

LOL, love that!

 

Well the use of a class doesn't mean its OOP

for example, would you call this OOP ?

<?php
class test1{
var $user = "";
var $pass = "";
function login($user, $pass){
	$this->user = $user;
	$this->pass = $pass;
	if("MadTechie" == $this->pass){
		echo $this->user." Logged in";
	}else{
		echo $this->user." NOT Logged in";
	}
}
}
$test = new test1;
$test->login("John","MadTechie");

Link to comment
Share on other sites

Not really, I would call that a waste of a class as you could clearley just use the function / if statment, noting is inherited either so it doesn't really add anything that a function would not be capable of anyway. It is hard to relate a definition to OOP, I guess that the only way of truly interpreting it is to understand yourself when to use certain concepts. I couldn't really explain OOP to someone hence the suggestion to visit wikipedia.

Link to comment
Share on other sites

not having inheritance and using OOP are not mutually exclusive. Hell not having classes and using OOP are not mutually exclusive either. Look at Javascript, its OOP system is prototypal (is that a word) rather than classical.

 

Oh, and why should you use OOP? because it rocks.

 

But in all seriousness, its not about one being better than the other. Its about what tool is best for the job. The whole hammer vs. screwdriver vs. wrench debate.

Link to comment
Share on other sites

So the way I looked at it, If i have a set of common data that will share common functions that the rest of the code won't use.  And I have the intent of REusing this (object) then, an object should be used.

 

For example, I made a paypal buyer object.  All the buyer information is related, and there are a few functions that pay requires so that the buyer information is correct.  But no where else in the code are those functions needed.  So an objected seemed the right choice.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.