PHP Namespaces

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
Reaction score
13
So I'm struggling here a bit. Bare with me.

PHP:
namespace AcidRaZor\Funky;

class WhatUP{

}

This lives in file awesome.php

I have another file, Autoloader.php:

PHP:
namespace AcidRaZor\Funky;

class Autoloader {

}

Within this class there's a "register" and "autoload" function which basically will load anything with the namespace that begins with AcidRaZor automatically.

However, I get Class 'Autoloader' not found when I try this:

PHP:
require_once 'Autoloader.php';
Autoloader::register();


WHY.

Driving me crazy. Not that I wasn't crazy. BUT I NEED TO GET THIS RIGHT. It's keeping me up at night. Need to use namespaces it looks awesome...
 
Code:
<?php namespace foo;
  class Cat { 
    static function says() {echo 'meoow';}  } ?>

file2:
<?php namespace bar;
  class Dog {
    static function says() {echo 'ruff';}  } ?>

file3:
<?php namespace animate;
  class Animal {
    static function breathes() {echo 'air';}  } ?>

file4:
<?php namespace fub;
  include 'file1.php';
  include 'file2.php';
  include 'file3.php';
  use foo as feline;
  use bar as canine;
  use animate;
  echo \feline\Cat::says(), "<br />\n";
  echo \canine\Dog::says(), "<br />\n";
  echo \animate\Animal::breathes(), "<br />\n";  ?>
 
Yea I saw that on the PHP website too. So you're saying I have to give the file I use:

PHP:
require_once 'Autoloader.php';
Autoloader::register();

a namespace like:

PHP:
namespace AcidRaZor\Whatever;
require_once 'Autoloader.php';
Autoloader::register();

Because I tried including my "awesome.php" file and referencing WhatUP with
PHP:
$elo = new WhatUP();

But that gave me the same error of Class not found
 
no that didn't work, I just got "Fatal error: Class 'Eish\Autoloader' not found" when I added the namespace "eish" to the index.php file that calls the Autoloader.php file to fire and include awesome.php automatically.
 
Figured it out (with no help from PHP website btw)

If I have a namespace AcidRaZor\Funky; and I have a file doing that include, it should be:

PHP:
namespace AcidRaZor\Funky; //same as the namespace everywhere else, not a random one like in file4.php of that example
require_once 'Autoloader.php';
Autoloader::register();
 
PHP:
require_once 'Autoloader.php';
\AcidRaZor\Funky\Autoloader::register();

????
 
PHP:
require_once 'Autoloader.php';
\AcidRaZor\Funky\Autoloader::register();

????

I'll try that too, actually I did try AcidRaZor\Funky\Autoloader (but I don't think I tried it with the leading slash like you)
 
Cool that worked too :)

Ugh, I see now what semaphore was hinting at at the very end when cat/dog/animal was called....ffs
 
So I'm struggling here a bit. Bare with me.

PHP:
namespace AcidRaZor\Funky;

class WhatUP{

}

This lives in file awesome.php

I have another file, Autoloader.php:

PHP:
namespace AcidRaZor\Funky;

class Autoloader {

}

Within this class there's a "register" and "autoload" function which basically will load anything with the namespace that begins with AcidRaZor automatically.

However, I get Class 'Autoloader' not found when I try this:

PHP:
require_once 'Autoloader.php';
Autoloader::register();


WHY.

Driving me crazy. Not that I wasn't crazy. BUT I NEED TO GET THIS RIGHT. It's keeping me up at night. Need to use namespaces it looks awesome...

Probably cause you're not as awesome as you think? :whistle:
 
Top
Sign up to the MyBroadband newsletter
X