Solved

file is loaded but another error

Posted on 2014-04-23
12
217 Views
Last Modified: 2014-05-05
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
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 57

Assisted Solution

by:Julian Hansen
Julian Hansen earned 334 total points
ID: 40019354
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
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 40019894
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
 

Author Comment

by:rgb192
ID: 40021632
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 40021737
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
 
LVL 57

Assisted Solution

by:Julian Hansen
Julian Hansen earned 334 total points
ID: 40022044
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
 

Author Comment

by:rgb192
ID: 40031920
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40031959
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
 
LVL 57

Assisted Solution

by:Julian Hansen
Julian Hansen earned 334 total points
ID: 40032028
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
 

Author Comment

by:rgb192
ID: 40036089
<?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
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 334 total points
ID: 40036552
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
 

Author Closing Comment

by:rgb192
ID: 40043546
many of these comments brought more understanding about namespace/class/folder

thanks.
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 40043681
You are welcome - thanks for the points.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question