Jump to content

Difficult time understanding namespaces.


Go to solution Solved by iarp,

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/291933-difficult-time-understanding-namespaces/
Share on other sites

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)?

  • Solution

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 by iarp
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.