Page 1 of 1

Which one to use and why in OOP ?

Posted: Sat Feb 24, 2007 7:53 am
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 !!!!!!!!!!!!!!!!

Posted: Sat Feb 24, 2007 10:38 am
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.

Posted: Sat Feb 24, 2007 11:04 am
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 ???

Posted: Sat Feb 24, 2007 11:06 am
by feyd
I already answered that.

Posted: Sat Feb 24, 2007 12:05 pm
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.

Posted: Sat Feb 24, 2007 2:04 pm
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

Posted: Sat Feb 24, 2007 2:25 pm
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 ...

Posted: Sun Feb 25, 2007 8:39 pm
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.