...

View Full Version : [Mac]Simple PHP OOP Bank Class



Mac II
02-27-2011, 12:21 PM
<?php
class BankAccount {
public $accountname;
public $accountnumber;
public $accountmoneys;
public function __construct($accname, $accnumber) {
$this->accountname = $accname;
$this->accountnumber = $accnumber;
}
public function __changeName($newaccname) {
$this->accountname = $newaccname;
}
public function __changeNumber($newaccnumber) {
$this->accountnumber = $newaccnumber;
}
public function __giveMoneys($accmoneys) {
$this->accountmoneys = $accmoneys;
}
public function __user_giveMoneys($accmoneys2) {
$this->accountmoneys = $this->accountmoneys + $accmoneys2;
}
public function __takeMoneys($howmuch) {
$this->accountmoneys = $this->accountmoneys - $howmuch;
}
public function __user_takeMoneys($howmuch2) {
$this->accountmoneys = $this->accountmoneys - $howmuch2;
}
public function __getUserDetails() {
echo "<p>Name: ".$this->accountname."</p>";
echo "<p>Number: ".$this->accountnumber."</p>";
if(!is_numeric($this->accountmoneys)):
echo "<p>INVALID MONEYS!!!</p>";
else:
echo "<p>Moneys: ".$this->accountmoneys."</p>";
endif;
}
}


If you wanna post a improve , don't post it with words , just give new source.

Obv credits to me for making class!

Thanks!

Fou-Lu
02-27-2011, 02:31 PM
The use of public modifier on members HAS to go. PHP is datatype weak, you cannot leave its members exposed to public access.
You don't have any control of the data going through this class. Nothing is stopping me from withdrawing or depositing an amount of 'cat'. All data must be handled at the level of any public method call, and not at the level where you wish them to call. They are public for a reason.


/**
* @author Kevin Simpson
*/
class Account
{
private $mAcctNum;
private $dAcctBal;

public function __construct($mAcctNum, $dAcctBal = 0.0)
{
$this->setAccountNum($mAcctNum);
$this->setAccountBalance($dAcctBal);
}

public function getAccountNum()
{
return $this->mAcctNum;
}
public function setAccountNum($mAcctNum)
{
// The rules I have chosen are a simple 12 digit numerical string only.
if (!preg_match('#\d{12}#', $mAcctNum))
{
throw new InvalidArgumentException("Invalid account number");
}
$this->mAcctNum = $mAcctNum;
}

public function getAccountBalance()
{
return $this->dAcctBal;
}

public function setAccountBalance($dAcctBal)
{
if (!is_double($dAcctBal))
{
throw new InvalidArgumentException("Invalid account balance");
}
$this->dAcctBal = $dAcctBal;
}

// One to modify
public function transaction($dModifyBy)
{
if (!is_double($dModifyBy))
{
throw new InvalidArgumentException("Invalid transaction amount");
}

$this->setAccountBalance($this->getAccountBalance() + $dModifyBy);
}
}


Your naming convention leaves a little to be desired. Methods starting with a _ traditionally indicate that they are to be treated as private, and methods starting with __ indicate they are to be treated as magical. You should not be using __ method names unless you are intending to use the magical functionality, or willing to cause havoc in the case that a magical method of the same name is added by the core. The only two methods I write that begin with __ are __toArray and __cast, which I am hopeful will be implemented in the future.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum