Which one to use and why in OOP ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Which one to use and why in OOP ?

Post by PHPycho »

I am wondering to know which one to use and why in OOP
scenario:
suppose i had a class named category.class.php
1st method:

Code: Select all

<?php
class category
{
// without creating properties
function insertNew($catName,$catDesc,....)
{
//insert into database goes here...
}
}
?>
2nd method

Code: Select all

<?php
class category
{
// with creating properties
var $catName;
var $catDesc;
var ....;
function insertNew()
{
//insert into database goes here...
}
}
?>
3rd Method:

Code: Select all

<?php
class category
{
var $catName;
var $catDesc;
var ....;
//creating constructor
function category($catName,$catDesc,..)
{
$this->catName = $catName;
$this->catDesc = $catDesc;
.......
}
function insertNew()
{
//insert into database goes here...
}
}
?>
and i used this class in the category.php as
1st method:

Code: Select all

<?php
include "category.class.php";
//get the parameters from the user inputs
//creating object
$catOBJ = new category();
$catOBJ->insertNew($catName,$catDesc,....);
?>
2nd method

Code: Select all

<?php
include "category.class.php";
//get the parameters from the user inputs
//without creating obj
category::insertNew($catName,$catDesc,....);
?>
3rd method

Code: Select all

<?php
include "category.class.php";
//get the parameters from the user inputs
$catOBJ = new category($catName,$catDesc,....);
$catOBJ->insertNew();
?>
Above i mentioned the ways of writing classes and implementing it, whic is the best
method of applying the OOP method for sake of clarity..
Thanks in advance to all of you !!!!!!!!!!!!!!!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Without proper indentation, I'd say none of them. Code-style aside, I'd say the first method from both examples. It's the most modular. However, the logic of it comes to question. insertNew() creates a new category somewhere, yes? That's typically a job for a manager class, not the individual categories to know.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

1st method:
php:
<?php
include "category.class.php";
//get the parameters from the user inputs
//creating object
$catOBJ = new category();
$catOBJ->insertNew($catName,$catDesc,....);
?>

2nd method
php:
<?php
include "category.class.php";
//get the parameters from the user inputs
//without creating obj
category::insertNew($catName,$catDesc,....);
?>
among these two which one is effective to use ???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I already answered that.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

I am creating the class as per table name ie
suppose if i have a table 'category' then category.class.php
will be created and all the operation related to that category like update, insert,select,delete etc
is done in the page.
Can you again explain which one to use , as i am a new to OOP
and trying to do each n everything in OOP
Thanks again.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Take a look at what's referred to as CRUD (Create, Read, Update and Delete): http://en.wikipedia.org/wiki/CRUD_%28acronym%29
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Both of you examples are procedural, so why don't you just create a function:

Code: Select all

category_insert_new($catName,$catDesc,....);
Objects are data structures that only a defined set of functions are allowed to operate on. Functions are code that you pass data to. Pick the way you want to go ...
(#10850)
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

You might want to think about something like

Code: Select all

class category
{
 private $catName ;
 private $catDesc ;

 public function setName($name) { // set the name }

 public function setDesc($desc) { // set catDesc }

 public function insert() { // insert a new record }
}
Getters and setters will allow you to put some business logic into your objects like data validation. Even if all your function does is set a property and nothing else, still use it, that way you can add in logic for that property later. This way your insert, update etc. will all use the same set of validation tests or whatever. Using an outside function would mean repeating that logic in both your class and an external function.

Using a static method is ok as well, that is really just a matter of coding style preference.

If you are using PHP4 you can still do this, but it will take some self discipline to call the set methods instead of accessing the properties directly.
Post Reply