2

I have a directory include. In this directory, I have only one dir: HQF. In this directory, I have many files where I always declare them belonging to the HQF namespace like this:

<?php
namespace HQF;
class MyClass
{
}

And the file is called "MyClass.php". I'm trying to stick "a bit" to PSR0.

Everything works fine, and I've made my autoloader like this:

function __autoload($className)
{
    $className = ltrim($className, '\\');
    $fileName  = '';
    $namespace = '';
    if ($lastNsPos = strripos($className, '\\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\\', DS, $namespace).DS;
    }
    $fileName .= str_replace('_', DS, $className).'.php';
    $fileName = 'include/'.$fileName;
    require $fileName;
}

So when I need a class I just have to do: $m = new \HQF\MyClass(); and it works like a charm, including automagically the file "include/HQF/MyClass.php"

I have a problem with "pure" functions.

I've made a file called "include/HQF/mb_utils.php" where I want to put my mb_xxx function. NB: no classes, only functions.

I've tried all the following things without success:

  • $test=\HQF\mb_ucname('calling HQF/mb_ucname...');
  • $test=\mb_ucname('calling HQF/mb_ucname...');
  • use \HQF\mb_utils; and then the two tests above;

None of them work. What am I missing?

2
  • 2
    You're missing the fact that __autoload() only works with classes, not with functions, namespacing is irrelevant... if you want to autoload functions, you need to do this via regular (manual) includes Commented Jan 2, 2015 at 18:22
  • I'd be interested in any responses suggesting how to approach this, because I'm currently trying to fathom a solution myself Commented Jan 2, 2015 at 18:24

1 Answer 1

1

I'm relying on Mark Baker's comment.

So here's my workaround: I've made a file named Mb.php and in it, a class named "Mb" (for 'multibytes') and I've put all my mutibytes functions like this:

<?php
namespace HQF;

class Mb
{
    static public function ucfirst(&$string, $e ='utf-8')
    {
        /* blabla */
    }
    static public function ucname($string, $e ='utf-8')
    {
        /* blabla */
    }
}

And then I call them like this:

$mystring_to_change = \HQF\Mb::ucname($original_name);

This is the only way I've found, I dont know if it's the best one... any suggestion welcome.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.