...

View Full Version : Which one to use and why in OOP ?



PHPycho
02-24-2007, 02:26 PM
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:
php:

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

2nd method
php:

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

3rd Method:
php:

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

3rd method
php:

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

Fou-Lu
02-25-2007, 03:17 AM
This depends 100% on your usage. With php 4... its not as important (still important, but not as important :P), but with php 5 it has a much stronger OOP engine.
Your attributes or variables are what the object is using.
Your function is your behaviour.
Now, I don't see full code, so I know this isn't a safe assumption to make. But to me it looks like you are treating this class as a static member, which means your second calling method is the only one that will work.
The purpose of the object is to... well... create an object. A single instance of an entity that you can manipulate as you see fit. If this was a class to say... connect to a database, you would probably create a new instance of the object (new Object()). Since it appears that you can use the entire functionality without creating an object (that is, statically), I don't really see why you would need to create a new instace of the object.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum