in lower level languages linke C where you need to be concerned about memory management classes and objects give you the opportunity to declare a collection of variables all as one group and then when you are done to clear that memory chunck... much more efficent than personally managing each seperate variable... in php you do not have to be concerned with your own memory management
php before 4.3.0 will not have the user of private and public areas... this lets you decalre and use variables that cannot escape the function.. useful in design that makes classes worthwhile, but you cannot do that right now...
other than that the use of objects is basically a design tool... lets you think and design your program around collections of info rather than having to keep track of several sets of variables.... its very easy to make one declaration 'new Obj()' and then to really have declared a bunch more variables... or simulate with a bunch of functions passing around a reference to a collection of objects...
if you are in the design stage... it makes it easier for you the programmer to think in objects...
punchline: you are right... functions are powerfull enough to get the job done... but by that argument... you should just write your own assembly code for everything as it is more efficient...
in reality these have the same effect:
compare a bunch of functions passing around an associative array by reference, and an object.... the more complex your object gets the better the pbject options appeals to me as a time saver
function newObj()
{
return array(
"link" => 'www.google.com',
"query_string" => array();
);
}
function addQueryVar( &$obj, $var_name, $value ) {
$obj['query_string'][$var_name] = $value;
}
function getQuery( &$obj ) {
$query = $obj['link'] ."?";
foreach ( $obj['query_string'] as $var_name=>val ) {
$query .= "{$var_name}={$val}&";
}
return $query
}
class httpQuery()
{
var $link;
var $query_string;
function httpQuery()
{
$this->link = 'www.google.com';
$this->query_string = array();
}
function addQueryVar( $var_name, $value )
{
$this->query_string[$var_name] = $value;
}
function getQuery()
{
$query = $this->link ."?";
foreach ( $this->query_string as $var_name=>val ) {
$query .= "{$var_name}={$val}&";
}
return $query;
}
}
$myQuery = newObj();
addQueryVar( $myQuery, 'var1', 'val1' );
addQueryVar( $myQuery, 'var2', 'val2' );
$query = getQuery( $myQuery );
$myQuery = new httpQuery();
$myQuery->addQueryVar( 'var1', 'val1' );
$myQuery->addQueryVar( 'var2', 'val2' );
$query = $myQuery->getQuery();