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
LVL 1
rgb192Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Julian HansenConnect With a Mentor Commented:
Yes AND the namespace declaration inside the class file must be the same.

If you have the folder the same but have
writer.php
<?php
namespace util;
...

Open in new window


It will also fail.
0
 
Julian HansenConnect With a Mentor Commented:
It won't find the class because you have changed the namespace.

It won't find the class because you have changed the namespace.

Firstly I am assuming your write.php should be writer.php - or else the file would not load.

Assuming the above then there are two issues

1. the spl_autoload_register and how it finds a file. It uses the util/writer as a folder (util) file (writer.php) to find the file

2. The instantiation of the object new util/Writer() - assumes the class definition has already been loaded - looks for the class in namespace util - but won't find it.
0
 
Ray PaseurConnect With a Mentor Commented:
Does Class Writer have an __autoload() (magic) method?

Please see the man page:
http://php.net/manual/en/function.spl-autoload-register.php

I'm not sure I understand the intent of calling spl_autoload_register() with no arguments.  Why are you doing that?  What do you expect to happen?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
rgb192Author Commented:
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.
0
 
Ray PaseurConnect With a Mentor Commented:
do not know why spl_autoload_register has no arguments
Well if you don't know why the code is written this way, certainly we do not know either!  I would never even try to test any code that I did not (at least think) I understand.  How would you know if the code worked to your expectations, if you did not know what to expect?

Don't just copy code -- read it for understanding and then try applying the principles it illustrates.  Change the variable names and change the input data to something you expect -- to produce a predictable output.  This is a better way to learn, better than copying.  Application is the key to understanding.
0
 
Julian HansenConnect With a Mentor Commented:
spl_autoload_register without arguments will look at your class instantiations and attempt to load the class based on the namespace and class arguments.

By changing the namespace you are confusing the function - it won't find your class.

It is like putting a file in a folder

data/input.txt

Referring to this in your code - and then renaming the folder and wondering why your file no longer loads.

In my first point I highlight the fact that in your post you specified the filename as write.php instead of writer.php. This was important because the filename has to match the name of the class. The function looks for a folder with the same name as the namespace and in that it looks for a file with the same name as the class you are trying to instantiate.

Once it finds that it expects that the namespace declaration inside the file will be the same one as used to instantiate it (refer my folder analogy above) AND that a class with the specified class name in the instantiation also is defined in that file.

In order for auto load type functions to work there needs to be a convention for how to specify where to find the code. You want to be able to logically organise your code on disk so provision has to be made in the autoload for this. This is done by

Namespace = equates => folder [util]
Classname = equates => file [writer]

Open in new window

In Class file
namespace [SAME AS SPECIFIED IN INSTANTIATION => util] {
  class [SAME AS SPECIFIED IN INSTANTIATION => writer] {
     ....
  }
}

Open in new window

As soon as you go changing any of the above you break the code - or more accurately you create a situation where the interpreter is looking in the wrong place for your code.
0
 
rgb192Author Commented:
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
0
 
Ray PaseurCommented:
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
0
 
Julian HansenConnect With a Mentor Commented:
What I was  trying to say in my previous post is that the spl_autoload_register function will attempt to work out where your class files are located based on the namespace name and the class name.

What this means is that the function will use the namespace name to find the folder where the class file is located. So if you are using a namespace util the spl_autoload_register will look for a folder called util.

If your class is called Writer then spl_autoload_register will look for a file called writer.php in the folder util - and will then load that file.

If you change your namespace to utilFAKE - they n you are going to need to rename the folder util to utilFAKE for it to work.

so if you have
<?php
spl_autoload_register();
$writer = new util\Writer();

Open in new window

Then your folder structure must be like so
util/
    writer.php 

Open in new window

Inside writer.php your code should look like this
<?php
namespace util;
class Writer {
}

Open in new window


If you change your namespace to uitlFAKE
<?php
spl_autoload_register();
$writer = new utilFAKE\Writer();
?>

Open in new window

then you must do the following

1. Change your folder name to utilFake
utilFAKE/
   writer.php

Open in new window


2. Change your writer.php code (inside the folder utilFAKE) to look like this
<?php
// CHANGE THE NAMESPACE HERE
namespace utilFAKE;
class Writer {
}

Open in new window

0
 
rgb192Author Commented:
<?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?
0
 
rgb192Author Commented:
many of these comments brought more understanding about namespace/class/folder

thanks.
0
 
Julian HansenCommented:
You are welcome - thanks for the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.