oop vs function

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
buto
Forum Newbie
Posts: 1
Joined: Thu May 17, 2007 1:43 pm

oop vs function

Post by buto »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi all,
if you please help me
i use functions to deal with mysql and every file contain one function 
when i want to do function i recall it from its file like next :
---------
1- config.php

Code: Select all

<?
    $dbname='any';
      $dbhost='localhost';
      $dbuser='any';
      $dbpw='any';
?>
---------------------------------
2- insertdata.php

Code: Select all

<?
include('config.php');

?>
<?
   function tellusss(){
global $ipt,$datetimet,$dbhost,$dbuser,$dbpw,$dbname,$namet,$emailt,$subjectt,$messaget;
$link=mysql_connect($dbhost, $dbuser, $dbpw) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$result = mysql_query("INSERT INTO tellus (ipt,datetimet,namet,emailt,subjectt,messaget)
VALUES ('$ipt','$datetimet','$namet','$emailt','$subjectt','$messaget')",$link);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}
  }
?>
------------------------------------------
3- index.php

Code: Select all

<?
include('config.php');
include('insertdata.php');
?>
<?
 tellusss();
?>
---------------------------
i have 2 question please :
1- is it a good way ?
2- how could i use object oriented instead of functions ( i read object oriented but i want an example ) or any helpfull thing ?
thanks alot and i wait answer


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

[quote="[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1"][b]3.[/b] Do not make multiple, identical posts. This is viewed as spam and will be deleted.[/quote]
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

What you have done is not good. Global variables are something to avoid because it makes your code less predictable. If you are serious about learning OO pick up some books. these two are good.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

The first thing to do is remove the dependency on global variables in your function implementations.. Whenever someone wants to use your function he only has to provide parameters as defined in the function signature (and doesn't have to care about less obvious requirements like the existence of global variables)...

Code: Select all

function TellUs($ip, $name, $subject, $message)
{
 // ...      btw, you still need to prepare your values for use in a mysql query... -> mysql_real_escape_string
}
// from here on it's more a matter of personal taste...

Since you also require a $link to actually perform the query you could factor that into a function too:

Code: Select all

function GetLink($dbhost, $dbuser, $dbpw, $dbname)
{
 $link = mysql_conect($dbhost, $dbuser, $dbpw);
 mysql_select_db($dbname, $link); 
 return $link;
}
Usually you don't want to bother to pass in the parameters since they come from your configuration file... Instead of defining variables in an config file i prefer to wrap them in a (parameter less) function...

Code: Select all

function GetDbName() { return 'dbname'; }
function GetCredentials() { $credentials = array(); $credentials['username'] = 'tim'; $credentials['password'] = 'password'; return $credentials }
And i usually add a confighelper too:

Code: Select all

GetConfiguredLink()
{
 $credentials = GetCredentials();
 return GetLink( .., $credentials['username'], $credentials['password'], .. );
}
(If you're going the OOP way, you'd probably have three classes, with little code for now, namely: a Domain Object with TellUs, a Configuration Object with GetDbName etc, and a DataBase object with the GetLink etc...)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

timvw's advice is good. But

Code: Select all

function GetCredentials() { $credentials = array(); $credentials['username'] = 'tim'; $credentials['password'] = 'password'; return $credentials }
can be written as

Code: Select all

function GetCredentials() { 
    return array('username' => 'tim', 'password' => 'password');
}
temp variables should be avoided.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Lots of possibilities and ways to improve or extend.. It's more about a concept than actual code ;) (In this case i just wanted to show that he can choose to implement a function for each value, or choose to return multiple values (that logically adhere to each other) at once...)

On the other hand, while i was writing the code, i knew there was a shorter way, just couldn't remember it, and didn't feel like looking it up (uh-oh.. it's been far to long since i've done anything serious with php)
Post Reply