Jump to content

why oop needed for php programming?


zgkhoo

Recommended Posts

That's hardly an accurate description. If methods were by definition static, I would agree. But they're not.

 

It's impossible to accurately describe OO in one sentence. If I would have to describe the main diff between OOP and procedural, I'd say it's in the structuring of native data and execution flow. But that's just just an approximation which is of little use to people who do not know OO.

 

OP should do a tutorial.

Link to comment
Share on other sites

OOP is putting a group of related functions in a class...then you can reuse that class later and whatever is in it.

HMM you can also reuse functions inst it?

hahahaha,

i meant oop consist of wat technique...

MAY I KNOW WHAT IS YOUR NATIVE LANGUAGE? lol

 

OK IMO no opinion lol

 

whatever you can do using class can also be done in ordinary functions

i also wonder whats with the class OK extends hmm you can also obtain that

using ordinary function just include those functions and call it within that

functions etc...

but i think variable handling is the main diff eg

 

assume this is in the class

var $variable ='teng';

function echome(){

echo this->variable;

}

 

now if you call a class you can still change the value for that

simply

$class->variable ='ay mali pala';

$class->echome(); //echoay mali pala

but using functions or a group of function in one page to replace class

you you have to declare every variables inside your functions as global to use it

 

eg.

assume this is inside the functions page

<?

$x=1;

 

function echome(){  global $x;

echo $x;

}

$x =9;

echome();

?>

 

Link to comment
Share on other sites

@teng84, Simply because your using classes does not mean your programming in OOP.

 

whatever you can do using class can also be done in ordinary functions

 

In your limited view maybe, but you don't understand what OOP is. As 448191 has stated...

 

It's impossible to accurately describe OO in one sentence. If I would have to describe the main diff between OOP and procedural, I'd say it's in the structuring of native data and execution flow. But that's just just an approximation which is of little use to people who do not know OO.
Link to comment
Share on other sites

@teng84, Simply because your using classes does not mean your programming in OOP.

 

whatever you can do using class can also be done in ordinary functions

 

In your limited view maybe, but you don't understand what OOP is. As 448191 has stated...

 

It's impossible to accurately describe OO in one sentence. If I would have to describe the main diff between OOP and procedural, I'd say it's in the structuring of native data and execution flow. But that's just just an approximation which is of little use to people who do not know OO.

@thorpe show your unlimited views instead of commenting that way  ;D

Link to comment
Share on other sites

Basically OOP is a style of programming.  Programming can be done in many different techniques.

OOP is a programming methodology that gained populatiry based on the fact that a lot of high level languages

are full oop (forgive me if I am wrong on that one sentence).  The idea behind OOP is the theory behind it.  Using objects/classes

in a specifically organized manner to make code more re-usable, maintanable, and easier to read.  Mixed together with a good

programming pattern (MVC, Factory, or what heve you) it can be a very powerful force that can save hours (if not days) off overall

development time.

Just like OOP procedural is it's own style.  Also referred to as "Sphagetti code" but often looked down on because it's all over the place, hard to read, hard to maintain, and even harder to keep up with. OOP is generally module based programming allowing you to add/remove modules from a system without affecting the core code.  This gives you a chance to have a lot easier time updated/upgrading a code.  Those the reason 90% of all 'good' third party and open source systems (no matter what the langauge) are done using OOP methodologies.  BEcause it would be a lot more cumbersome, and difficult to manage if it was done in standard procedural programming.  I also believe procedural has it's place (for example in cake and code igniter some of there helpers are written procedural) those are perfect situations where procedural can be really helpful.

Link to comment
Share on other sites

Spaghetti code !== procedural

 

Spaghetti code simply refers to code where the execution flow is unclear because it seems 'tangled', moving back and forth without obvious structure. You can write bad code like that using any paradigm, OOP included. That said, one could state that OOP provides more structure and is thus less prone to problem. Lower level languages like Assembly are, under the same assumptions, more prone to the problem.

 

 

 

Link to comment
Share on other sites

Well, I've always considered OOP an additional layer of abstraction using code structure. It offers little/no functionality to the code or page itself, just structure and clarification, modularity and inheritance. It favors large projects where reused code will save a lot of space/coding time. This is important because during actual runtime object oriented PHP code takes longer to execute than flat scripted code which does the same thing.

 

The argument between OO and procedural is one of those never ending battles between warring CS factions and code developers. The only advise I could give is just never make objects for the sake of making objects.

Link to comment
Share on other sites

I like cmgmyr's brief description

OOP is putting a group of related functions in a class...then you can reuse that class later and whatever is in it.

 

Of course it sounds so much better if instead of classes you refer to them objects, which is what they're going to be anyway.

 

Imagine you made an entire ShoppingCart class

 

hell, it might even refer to a bunch of other classes inside of it....for instance a Product class or object.....to go by what I said earlier.

 

The idea here is to be able to create an entire Shopping Cart with just 5 or 6 lines.....not including the lines that make up the ShoppingCart class file.

 

then in your PHP you say...well I'll have my Shopping Cart about right here

<?php

include 'ShoppingCart.class.php';

$theCart = new ShoppingCart();

//then as you add more products to your cart...assuming you design you Shopping Cart class this way

$theCart->addProduct(unserialize($_GET['pid']));

//where $_GET['pid'] is a serialized version of a Product object on some other page

 

it's much better to have one single object to send, rather than a hundred or so $_POST variables

Link to comment
Share on other sites

No-one has given the definition of the acronym we are using, this :

OOP = object oriented programming.

 

Another word you might like to use in the same context would be "flexibility". The ability to interchange objects without having to modify the underlying code structure.

Link to comment
Share on other sites

Ah hah, the crux of the real problem. Design patterns. Without these patterns OOP is fairly meaningless. Who can tell me what the pattern i'm using below is called?

 

<?php
// Actor
class actor {

  private $speech;
  
  public function __construct(){
     $this->speech = defaultSpeech()
  }

  public function changeSpeechBehaviour(speechBehaviour $behaviour){
    $this->speech = $behaviour;
  }

  public function speak(){
    $this->speech->speak();
  }
}

// Speech Behaviour interface
interface speechBehaviour {
  public function speak(); 
}

// Basic english speaking.
class defaultSpeech() implements speechBehaviour {
  public function speak(){
    echo "I am an outspoking english gentleman, ahem!";
  }
}
// Uber speaking.
class l33tSpeech() implements speechBehaviour {
  public function speak(){
    echo "I speek ur ub3r l33t sp34k.";
  }
}

// RUNTIME
$charles = new Actor();
$charles->speak();
$charles->changeSpeechBehaviour(new l33tSpeech());
$charles->speak();
?>

 

Copy/ paste see what you get, and then don't tell me OOP doesn't have its uses.

Link to comment
Share on other sites

You do realise "Design Patterns" are simply identifiable/recognised common scenarios, right? They are not their own product, thus OOP is not "useless" without them..

 

Besides which, your Actor's constructor breaks the whole reason you are using that pattern in the first place.. you should be using a Factory to provide the default speech object, then your Bridge pattern would be complete. :)

Link to comment
Share on other sites

I never said "useless" i said meaningless ;), nevertheless you are correct they are just common scenaries.

Nevertheless what I have provided is a simplistic example of a particular pattern, however I feel it demonstrates the point that objects are interchangeable and flexible.

Incidently the line:

$this->speech = defaultSpeech();

should be

$this->speech = new defaultSpeech();

Link to comment
Share on other sites

  • 3 weeks later...

Here's an attempt to answer the OP's question, rather than concerning myself with many of the side-arguments that are evolving here.

 

why oop needed for php programming?

It's not; every PHP-based web site can be written procedurally without a single line of object oriented code.

 

is it any special with oop?

The shortest answer is: OOP makes your life easier; you should always be interested in things that make your life easier.

 

Given that there are hundreds of books on the subject, there is absolutely no way all of your questions about OOP can be answered in a single thread.  I suggest you do some independent research on the subject via internet tutorials, articles, etc. or just buy a book on the subject.

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.