...

View Full Version : OOP Usage



WingTsun
02-02-2012, 02:30 AM
Hello guys. Just passed my first year of working as a web-site programmer (Self Learned). It is time for me to upgrade, and now I will start to write in OOP Only.

Here is my first try to use OOP PHP Classes. Now the classes have just basic function, later I will add all the necessary functions. Now what I want is - Can you guys check this out, and give me suggestions how to do this better.. Since I am learning OOP just by google, I am sure I am not doing this as it should be..


<?

# EXCHANGE CLASS

class exchange {

public $method;

function rewrite_mehod_name_full() {

global $global;

$method = $this -> method;
$method = $global['global']['payMethod_full'][$method];
return $method;
}

function rewrite_mehod_name_short() {

global $global;

$method = $this -> method;
$method = $global['global']['payMethod_short'][$method];
return $method;
}

function rewrite_mehod_currency() {

global $global;

$method = $this -> method;
$currency = $global['global']['payMethod_currency'][$method];
return $currency;
}
}

// Define the Object alrady here, and include this in files where its necessary

$objekts = new exchange;
$objekts2 = new exchange;


// On other pages I use Objects like this ..

$objekts -> method = $bill['from_method'];
$objekts2 -> method = $bill['to_method'];
echo $objekts -> rewrite_mehod_name_short();


# BILLS CLASS

class bill {

public $id;

function select_bill() {

$bill = mysql_fetch_array(mysql_query("SELECT * FROM `bills` WHERE `id`='".$this -> id."'"));
return $bill;
}

function show_description() {

global $website;

$data = $this -> select_bill();

$obj_from = new exchange;
$obj_to = new exchange;
$obj_from -> method = $data['from_method'];
$obj_to -> method = $data['to_method'];

# WMX - BANK

if(exchange_type($data['from_method'], $data['to_method']) == 1) {

return 'NR: ' .$data['nr'] . ' Withdrawal: ' . $data['BANK_IBAN'] . ' '. format_2($data['from_amount']) . ' '. $obj_from -> rewrite_mehod_name_short() . ' Total: '. format_2($data['to_amount']) . ' '. $obj_to -> rewrite_mehod_currency() . ' Fee: '. format_2($data['my_cut']) . ' '. $obj_to -> rewrite_mehod_currency() . ' | '. $website['domain'];
}

# WMX - WMX

if(exchange_type($data['from_method'], $data['to_method']) == 2) {

return 'NR: ' .$data['nr'] . ' '. format_2($data['from_amount']) . ' '. $obj_from -> rewrite_mehod_name_short() . ' -> '. format_2($data['to_amount']) . ' '. $obj_to -> rewrite_mehod_currency() . ' | '. $website['domain'];
}

# BANK -> WMX

if(exchange_type($data['from_method'], $data['to_method']) == 3) {

return 'NR: ' .$data['nr'] . ' '. format_2($data['from_amount']) . ' '. $obj_from -> rewrite_mehod_currency() . ' -> '. format_2($data['to_amount']) . ' '. $obj_to -> rewrite_mehod_name_short();
}

# WN - WMX

if(exchange_type($data['from_method'], $data['to_method']) == 4) {

return 'NR: ' .$data['nr'] . ' '. format_2($data['from_amount']) . ' '. $obj_from -> rewrite_mehod_currency() . ' -> '. format_2($data['to_amount']) . ' '. $obj_to -> rewrite_mehod_name_short() . ' | '. $website['domain'];
}

}

}

// Make Object

$obj_bill = new bill;

// This is how I use this on the necessary pages

$obj_bill -> id = 286;
echo $obj_bill -> show_description();

?>

Fou-Lu
02-02-2012, 05:20 AM
Drop your use of global; it creates a debugging nightmare when it can be altered directly from any scope. Pass this value as a parameter either during construction or through method calls themselves.

PHP is datatype weak. You should never expose a property as being public since you cannot control what data goes into it. Always use accessors and mutators.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum