Jump to content

adding a property in a class


justin7410

Recommended Posts

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,

 

 

 
Link to comment
Share on other sites

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";
    } 
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.