Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    [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!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •