Jump to content

Hubble

New Members
  • Posts

    6
  • Joined

  • Last visited

Everything posted by Hubble

  1. Thanks. My finished code (copy and pasted so it looks horrible): $yes = "yup"; $no = "no"; $arr = array("yes" => $yes, "no" => $no); $obj = new test($arr); class test { public function __construct(Array $arr) { foreach($arr as $key => $value) { $this->{$key} = $value; } $this->init(); } public function init() { echo $this->yes; } }
  2. A better way of explaining it: I want to create properties in a class with the names of all of the variables passed to the constructor and with the same values.
  3. Ok, so here's an example: a.php: <?php include 'b.php'; $yes = "yes"; $no = "no"; $maybe = "maybe"; $obj = new b($yes, $no, $maybe); It passes 3 variables to b(). I want to have other things like that which will pass different amounts. b.php: class b { public function __construct() { $args = func_get_args(); // Loop through all variables somehow $this->{all variables} = {value of all variables} // Create properties of each variable with their values $this->example(); } public function example() { echo $this->yes; // This should output "yes" } }
  4. I know how to dynamically create class properties like this: class Sample { /* Creates instances of classes and dynamically assigns them to properties * Example: If "test.php" is in the Classes folder, an instance of its class ... * ... will be assigned to $this->test. */ public function createInstances() { foreach(glob("Classes/*.php") as $file) { require_once $file; $fileName = basename($file, ".php"); if(class_exists($fileName)) { $lclass = strtolower($fileName); $this->{$lclass} = new $fileName(); } } } } $obj = new Sample(); $obj->createInstances(); And I'm using this in my project. But there's something I want to make using the same sort of principle - I want to dynamically create properties from all of the arguments passed to the constructor. The thing is that I don't know how many arguments will be passed. I think you can use `for` loops for this kind of thing, but I'm not sure how I would go about doing that.
  5. I changed it to basename($file, ".php") and I still get the error. EDIT: It works now, I also had a few other mistakes which I fixed.
  6. 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!
×
×
  • 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.