iarp Posted October 20, 2014 Share Posted October 20, 2014 I'm not entirely sure what is wrong with me, but I'm having a super difficult time understanding namespaces. I have 2 primary classes # Resides within class.DataTypeBase.php namespace DTB\Base; class DataTypeBase { protected $name = ''; protected function getName() { return $this->name; } protected function setName($name) { $this->name = $name; } } # Resides within class.DataType.php namespace DTB; class DataType extends DataTypeBase { public function process_data(){ .... } } I then have a third file that contains approximately 9 different classes that further extend DataType # Resides within class.DataTypes.php namespace DataTypes; class Silver extends DataType { public function differ($type) { .... } } The namespace system seems to work for class DataTypes, but with the class Silver I get an error message saying that the class DataType does not exist. From what I read online, and using the example above I figured the "extends DataType" may have to be "extends \DTB\DataType" but it results in the same error message. I'm not entirely sure what I need to do for this to work properly Quote Link to comment Share on other sites More sharing options...
requinix Posted October 20, 2014 Share Posted October 20, 2014 I'm inclined to think you're using DTB and DTB\Base incorrectly, but whatever. Since Silver is in a different namespace than DataType, you need to use either \DTB\DataType or a use to that extent. If you're still getting errors, are you sure your autoloader is set up correctly? Well, since you're putting many classes into one single file (bad practice, by the way), it's more likely that you're require_once()ing the right file(s)? Quote Link to comment Share on other sites More sharing options...
Solution iarp Posted October 22, 2014 Author Solution Share Posted October 22, 2014 (edited) I had originally just been require_once on all of the required files within my config file which is the first point of entry into the application. I had not been using an autoloader until you mentioned it. I remember that phpmailer uses one so I copied and modified what they used and the result was: <?php function DTBAutoload($class) { $filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php'; #echo $filename . '<br />'; if (is_readable($filename)) require $filename; } if (version_compare(PHP_VERSION, '5.1.2', '>=')) { if (version_compare(PHP_VERSION, '5.3.0', '>=')) { spl_autoload_register('DTBAutoload', true, true); } else { spl_autoload_register('DTBAutoload'); } } else { /** * Fall back to traditional autoload for old PHP versions * @param string $classname The name of the class to load */ function __autoload($classname) { DTBAutoload($classname); } } It required a bit of file and folder renaming to get it correct, plus separating each of the classes into their own separate files. Afterwards the \DTB\ namespace started working everywhere. Edited October 22, 2014 by iarp Quote Link to comment 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.