Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

file is loaded but another error

listing5.14.php
<?php

spl_autoload_register();
$writer = new util\Writer();
?>

Open in new window


\util\write.php
<?php
namespace utilFAKE;

class Writer {

}

?>

Open in new window

when I change
namespace util;
to
namespace utilFAKE;

file is loaded but why do I get this error:
Fatal error: spl_autoload(): Class util\Writer could not be loaded in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_05_Code\listing5.14.php on line 4
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

2 files are from matt zandstra php book

everything works until I change the namespace name

I do not know why spl_autoload_register has no arguments


Does Class Writer have an __autoload() (magic) method?
I am copying 2 files from a tutorial. I do not know.


$writer = new util\Writer();
is the 'util' a namespace
and 'writer()' a class

and how does php know to search all the files in the folder to find

/util/writer.php
I wrote /util/write.php which was incorrect.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

I do not understand julianH example of patterns of how to call class,namespace
because it is too philosophical.
Maybe I would learn better with a real example


please call file

\util\writer.php
<?php
namespace utilFAKE;

class Writer {

}

?>

Open in new window



because this code will call namespace util, but not namespace utilFAKE
<?php

spl_autoload_register();
$writer = new util\Writer();
?>

Open in new window


what change needs to be made to call namespace utilFAKE
Just an untested guess, but maybe worth trying this?

<?php

spl_autoload_register();
$writer = new utilFAKE\Writer();

Open in new window

Sidebar note: ALWAYS OMIT THE END-PHP TAG unless it is absolutely necessary.  Antipractice # 14
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

<?php

spl_autoload_register();
$writer = new utilFAKE\Writer();


Fatal error: spl_autoload(): Class utilFAKE\Writer could not be loaded in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_05_Code\listing5.14.php on line 4


Then your folder structure must be like so
so the namespace and foldername must always be same?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

many of these comments brought more understanding about namespace/class/folder

thanks.
You are welcome - thanks for the points.