Search the Community
Showing results for tags 'property'.
-
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,
-
I want to require_once all of the files in my directory and assign instances of their classes to properties in the main class. Main class (Test.php): <?php error_reporting(E_ALL); class Test { public $egg; public function __construct() { $this->getInstances(); $this->egg->msg("Hello!"); // line 11 } public function getInstances($folder = null) { if($folder != null) { foreach(glob("$folder/*.php") as $file) { require_once $file; $class = basename($file); if(class_exists($class)) { $lclass = strtolower($class); if(property_exists($this, $lclass)) { $this->$lclass = new $class(); } } } } else { foreach(glob("*.php") as $file) { require_once $file; $class = basename($file); if(class_exists($class)) { $lclass = strtolower($class); if(property_exists($this, $lclass)) { $this->$lclass = new $class(); } } } } } } $test = new Test(); ?> Sample class (Egg.php): <?php class Egg { public function msg($msg) { echo $msg . chr(10); } } ?> What it outputs: Fatal error: Call to a member function msg() on a non-object in Test.php on line 11 What I want it to output: Hello!