Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-27-2011, 12:21 PM   PM User | #1
Mac II
New to the CF scene

 
Join Date: Feb 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Mac II is an unknown quantity at this point
[Mac]Simple PHP OOP Bank Class

PHP Code:
<?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!
Mac II is offline   Reply With Quote
Old 02-27-2011, 02:31 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,750
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
PHP Code:
/**
 *  @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.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:59 PM.


Advertisement
Log in to turn off these ads.