Search the Community
Showing results for tags 'namespaces'.
-
In the "Namespaces" section in Ch.6, Larry describes how the use keyword can allow us to reference a namespace once in our script so that we don't need to name the specific namespace over and over again when we make objects: Unfortunately, I am getting a class not found error when I try to use this. See program below, a shorter version of the type hinting example used earlier in the book. Company.php <?php namespace MyNamespace\Company; class Department{ private $_name; private $_employees; function __construct($name){ $this->_name = $name; $this->_employees = array(); }//__construct function addEmployee(Employee $e){ $this->_employees[] = $e; echo "<p>{$e->getName()} has been added to the {$this->_name} department.</p>"; }//addEmployee }//Department class Employee{ private $_name; function __construct($name){ $this->_name = $name; } function getName(){ return $this->_name; }//getName }//Employee ?> Using use in a separate file in the same directory as Company.php: <?php require('Company.php'); use MyNamespace\Company; $hr = new Department("Human Resources"); $e1 = new Employee("mary"); $hr->addEmployee($e1); ?> The code above generates a fatal error: "Fatal error: Class 'Department' not found" Am I missing something or am I not using use properly? Any assistance is greatly appreciated.
-
Hey, everyone. Working on a pluginen for Wordpress right now. It will allow you to comment articles as a phpBB3 user. The problem has to do with phpBB3s native session solution. By including the file "common.php" and doing some authentification against phpBB3, you'll have support for phpBB3 sessions outside of the forum solution. The problem is that both phpBB3 and Wordpress are polluting the global namespace. Global variables such as $db, $theme, etc, is used by both system. We also have function naming conflicts. Functions are pretty manageable. Because I can run those few functions I need inside my own classes, this works fine. The problem is those global variables that crash WP when included from phpBB3. Is there a way to unset the global variables or a way to use PHPs new namespaces to solve this issue? I guess both PHP and JavaScript developers might be able to answer me that.