Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Calling Classes In Namespace
  • Hello; I have a class store in app/classes/myhtml.php the contents:
    namespace ClassName;
    Class myhtml () {
    ...
    } I will call this class in my view app/view/site.php $val = new ClassName_myhtml(); I get this error ErrorException [ Error ]: Class 'ClassName_myhtml' not found What could be the problem?
  • You haven't read the documentation on how namespaces and classnames work, and how the autoloader interacts with it. For the application, app/classes is the classes root. So classes in there are defined in the global namespace, and without a prefix. For a class in app/classes/example/test.php, you can choose to call it "Example_Test" in the global namespace, or "Test" in the namespace "Example". You can test if your class can be found using the following trick:
    - take the namespace and the classname together ( in the example \Example\Test )
    - replace all "\" and "_" by the directory separator ( example is now "/Example/Test" )
    - convert to lower case ( example is now "/example/test" )
    - class should be defined in app/classes/example/test.php
  • What should I change in the example given above? Sorry, your explanation isn't clear enough for me to understand. Since my example has a namespace then it should work in these circumstances? $val = new /ClassName/myhtml();
    $val = new ClassName_myhtml();
    $val = new \ClassName\myhtml();
    $val = new /classname/myhtml();
  • Your file is in app/classes. It therefore can NOT have a namespace. If you want the class in app, AND you want the namespace, it should be
    // namespaces uses backslashes, check the PHP documentation
    $val = new \Classname\Myhtml(); // both namespaces and classes should be used ucfirst()
    

    which whould map to
    <?php
    namespace Classname;
    
    class Myhtml {}
    

    in a file called app/classes/classname/myhtml.php (all lowercase).

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion