Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have very simple web app. Learning how to use namespaces.

Structure of directories is:
www (root)
class (application directory)
index.php
payment (sub-directory for namespace)
card.php (separate class, namespace)
gameplay (sub-directory for namespace)
card.php (separate class, namespace)


Code is as follows:

index.php
<?php 
	function my_autoload_function($c)
	{
		require_once "class/".str_replace("\\","/",$c).".php";		
	}

	spl_autoload_register('my_autoload_function');

	$gpcard=new \gameplay\card;
	$gpcard->play();
?>



card.php (subfolder payment)
<?php 
	namespace payment;
	class Card
	{
		public function pay()
		{
			echo "Plaćanje Visa karticom";
		}
	}
?>



card.php (subfolder gameplay)
<?php 
	namespace gameplay;
	class Card
	{
		public function play()
		{
			echo "Igra Dama crvena";
		}
	}
?>



Error messages which I get are:
1)
Quote:
Warning: require_once(class/gameplay/card.php): failed to open stream: No such file or directory in C:\wamp64\www\class\index.php on line 4

2)
Quote:
Fatal error: require_once(): Failed opening required 'class/gameplay/card.php' (include_path='.;C:\php\pear') in C:\wamp64\www\class\index.php on line 4



Can somebody help with this?

What I have tried:

I tried different solutions with paths but without success.
Posted
Updated 30-Sep-19 2:40am

1 solution

require_once needs the proper path. See an example here, doctrine orm - PHP namespaces in project subfolders - Stack Overflow[^]
 
Share this answer
 
Comments
Sinisa Janjetovic 30-Sep-19 9:44am    
Thank you for your help. I know that it was problem with path. Solved :-)
ZurdoDev 30-Sep-19 9:56am    
Glad to hear it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900