justin7410 Posted March 2, 2014 Share Posted March 2, 2014 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, Quote Link to comment https://forums.phpfreaks.com/topic/286661-adding-a-property-in-a-class/ Share on other sites More sharing options...
KevinM1 Posted March 2, 2014 Share Posted March 2, 2014 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/286661-adding-a-property-in-a-class/#findComment-1471269 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.