Jump to content

re-call autoload class in different folder


Yohanne

Recommended Posts

any idea how to call autoload class in different folder? ex. below that i have..

 

auto.php

<?php

require 'login/index.php';
 function __autoload($classname) 
	{
	$filename = "./../class/". $classname .".php"; include_once($filename);
	}
?> 

 login/index.php

<?php

$new_apptitle = new ClassAppTitle(); 	

?>

<html>
 <head>
   <title><?php $new_apptitle->get_apptitle(); ?></title>
 </head>
   <body>
   </body>
</html>


now, how do i re-call the $new_apptitle = new ClassAppTitle(); into different folder? let say like below..

 

tab/home.php

<?php

$new_apptitle = new ClassAppTitle(); 	

?>

<html>
 <head>
   <title><?php $new_apptitle->get_apptitle(); ?></title>
 </head>
   <body>
   </body>
</html>

Link to comment
Share on other sites

Your first problem is having more than one class with the same name. That's a no-no. Name each class differently because they are, in fact, different classes. Then rename their files appropriately and if necessary modify your autoloader to fit, since it now knows which classes you're talking about.

Link to comment
Share on other sites

i get lost the example that you recommend but by the way, could you elaborate more, or give me an example like structure above, in use of spl_autoload_regester.

There is an example on the page I linked you to. It's also a very well documented spec, where exactly are you lost?

Link to comment
Share on other sites

Your first problem is having more than one class with the same name. That's a no-no. Name each class differently because they are, in fact, different classes. Then rename their files appropriately and if necessary modify your autoloader to fit, since it now knows which classes you're talking about.

This is a non issue thanks to namespaces.

Link to comment
Share on other sites

Hi, i get same issue regarding spl_autoload_regester. it did not load file data. why?


class Action
	{
	public static function systemtitle($className)
	{
		$dataname = "../class/" .$className. ".php"; 	require $dataname;
	}
					
public static function dataParameter($classNamee)
	{
		$datanamee = "../data/" .$classNamee. ".php"; 	require $datanamee;
	}	
	}
spl_autoload_register('Action::systemtitle'); $datavaluee = new ClassData(); $datavaluee->classdatas();
spl_autoload_register('Action::dataParameter'); $datavaluee = new parameter(); $datavaluee->parameters();
Link to comment
Share on other sites

Are you sure you're not getting a "failed to open stream" error with require()? Because you should be getting at least one and that will kill your script.

 

Have you tried even the most basic of debugging techniques: printing stuff out and seeing what's happening? Because you're in a much better position to do that then we are, given that it's your code we're trying to help you with.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.