Jump to content


Photo

adding a property in a class

oop property variable global

  • Please log in to reply
1 reply to this topic

#1 justin7410

justin7410

    Advanced Member

  • Members
  • PipPipPip
  • 106 posts

Posted 02 March 2014 - 03:50 PM

Hey guys,

 

I am learning some basic OOP rules and application right now. 

 

I was trying to understand setting a property inside of an object.

 

$name = $_GET['user'];

if(isset($name)){


class BankAccount{
public $balance = 3500;

public function DisplayBalance(){
return  $name . ' your Balance Currently is: ' .$this->balance;
}

public function Withdraw($amount){
$balance = $this->balance;


if($balance < $amount){
$this->balance = 'Sorry you can\'t withdraw any funds right now, not enough to cover amount request of: $' .$amount . '. ';
}
else{
$this->balance = $this->balance-$amount;
  } 
 } 
}

So here i am trying to add an extra property into this class by grabbing the variable of $name, which is a dynamic string given from a user submit form.

 

When i add 

public $user = $name; || $user = $name;

I get an error, so my syntax is wrong

 

So ultimately my question is, how do i correctly add the variable of $name into my class ?

 

thanks guys,

 

 

 


#2 KevinM1

KevinM1

    Snarkimus Prime

  • Moderators
  • 5,196 posts
  • LocationNew Hampshire, USA

Posted 02 March 2014 - 04:23 PM

1. A class defines a type, which is both data and the series of operations that can be performed on that data. Example: integers. Integers are whole numbers that can be modified via arithmetic. Because you're defining a type, it really doesn't make sense to put a class definition within an if-conditional. I mean, you wouldn't hide the ability to create an integer behind some conditional, would you? Or a function definition?

2. Class properties should be labeled either protected (if you're going to subclass) or private. Why? It keeps you or someone else from accidentally overwriting them by ensuring that data manipulation happens only through clear lines of explicit communication.

3. You really shouldn't output in class methods. Methods should return values unless you're writing a class whose purpose is to handle displaying data.

So, with all that, I'd rewrite what you have as:
 
class BankAccount
{
    private $balance;
    private $name;

    public function __construct($balance, $name)
    {
        $this->balance = $balance;
        $this->name = $name;
    }

    public function getBalance()
    {
        return $this->balance;
    }

    public function setBalance($newBalance)
    {
        $this->balance = $newBalance;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($newName)
    {
        $this->name = $newName;
    }

    public function deposit($amount)
    {
        $this->balance += $amount;
    }

    public function withdraw($amount)
    {
        if ($this->balance < $amount) {
            throw new Exception;
        } else {
            $this->balance -= $amount;
        }
    }
}

$name = $_GET['user'];

if (isset($name)) {
    $account = new BankAccount(3500 /* or some other amount obtained from someplace (db?) */, $user);

    try {
        $account->withdraw(4000);
    } catch (Exception $e) {
        echo "You cannot withdraw more than what is in your account";
    } 
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com