SitePoint Sponsor

User Tag List

Page 3 of 3 FirstFirst 123
Results 51 to 60 of 60
  1. #51
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by brandlover.vn View Post
    Fatal error: Class 'NS1' not found in namespace1.php on line 15
    Autoload isn't designed to do what you're trying to, it's for classes only.

  2. #52
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 33degrees View Post
    Autoload isn't designed to do what you're trying to, it's for classes only.
    Thank you.

    I need a solutions for autoload namespace.

    PHP Code:
    use Namespace1// => auto include Namespace1.php

    // good
    Namespace1::test(); // => auto include Namespace1/test.php 

    http://www.onphp5.com/article/61
    PHP Code:
    <?php
    function __autoload($className) {
      
    // Assume that all class files are located in the same dir and subdirs
      
    $fname str_replace('::'DIRECTORY_SEPARATOR$className) . '.php';
      if(
    is_file($fname)) {
        include_once(
    $fname);
        return;
      } 
      
      
    $namespace substr($className0strrpos($className'::'));
      
    $localClassName substr($classNamestrrpos($className'::') + 2);
      if(
    $namespace) {
        eval(
    "namespace $namespace;
              class 
    $localClassName {
                function __construct() {
                  throw new Exception('Class 
    $namespace::$localClassName not found');
                }
            
                static function __callstatic(\$m, \$args) {
                  throw new Exception('Class 
    $className not found');
                }
              }"
    );
      } else {
        eval(
    "class $className { 
                function __construct() { 
                  throw new Exception('Class 
    $className not found');
                }
              
                static function __callstatic(\$m, \$args) {
                  throw new Exception('Class 
    $className not found');
                }
              }"
    );
      }
    }

  3. #53
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ideally, there should be some type of autoload that works with the "use" keyword, but you'd have to talk to the php devs about that (maybe create a ticket in their bug tracker?).

    The snippet of code that you posted could be repurposed to do what you want, by creating a dummy class and having __callstatic call your function. I don't know what kind of side effects it might have without trying it though.

  4. #54
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 33degrees View Post
    Autoload isn't designed to do what you're trying to, it's for classes only.
    Are you quite sure? Autoloading namespaced classes in the article under discussion.

  5. #55
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    require_once( dirname( __FILE__ ) . '/' . $classname . '.php' );
    As we're discussing PHP 5.3 here, remember you now have the __DIR__ "magic constant"

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  6. #56
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ezku View Post
    Are you quite sure? Autoloading namespaced classes in the article under discussion.
    brandlover.vn was trying to autoload a function, not a class.

  7. #57
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 33degrees View Post
    brandlover.vn was trying to autoload a function, not a class.
    Oh, that's true. So it's a problem, but not one we don't have already with 5.2, isn't it?

  8. #58
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still, there are a lot of areas to be improved in PHP.

  9. #59
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ezku View Post
    Oh, that's true. So it's a problem, but not one we don't have already with 5.2, isn't it?
    Exactly. The easiest solution is to simply not use functions, using static calls on classes instead...

  10. #60
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Or just don't use __aoutload Its really not that hard to include.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •