Jump to content

Recommended Posts

hello, all i am trying to learn php, and currently trying to get my head around using classes.

 

i have the following layout:

 

index.php

<?php 

include("config/autoloader.php");

$content = new content(); 
$content->get_content(); // loads Content! 
$content->edit_content(); // Edits Content!
?>
<?php
$navbars = new navbars();
$navbars->get_navbar1(); // loads navbar1!
?> 

 

config/autoloader.php

<?php

function spl_register_autoload($content) { 
   include_once("classes/" . $content . ".php"); 
} 

function spl_register_autoload($navbars) { 
   include_once("classes/" . $navbars . ".php"); 
} 

?>

content.php (content class file)

<?php
class content {
    
   public function get_content() { 
      echo "Show me the content!"; 
   }
   
      public function edit_content() { 
      echo "Edit the content"; 
   } 
}

?>

navbars.php navbars class file

<?php
class navbars {
      public function get_navbar1() { 
      echo "Show me the navbar1!"; 
   }
}

?>

 

i keep getting the 500 - internal error on iis, but works on a single class file, as soon as i add more then one i get the error, can someone please help me and explain where i have gone wrong.

 

thanks, all.

Link to comment
https://forums.phpfreaks.com/topic/251215-php-autoload-classes/
Share on other sites

You can't declare two functions/methods with the same name, in the same runtime namespace. You have to make each function name unique.

 

However, within classes, you can use the same method name as long as they are in separate classes - but I wouldn't recommend that; there is no reason that every function and method can't have there own name.

However, within classes, you can use the same method name as long as they are in separate classes - but I wouldn't recommend that; there is no reason that every function and method can't have there own name.

 

Disagree here. Using similar names for similar methods saves you from having to remember different naming schemes.

However, within classes, you can use the same method name as long as they are in separate classes - but I wouldn't recommend that; there is no reason that every function and method can't have there own name.

 

Disagree here. Using similar names for similar methods saves you from having to remember different naming schemes.

 

Tamato / tomato ..... True, there is a good consistency argument to be made here, however, can present some confusion for someone just starting to learn OOP.

i do have a small amount of knowledge on functions and them not been able to the same name, i have been doing my functions in one file.

the issue is my class file is getting rather big and hard to update and work with, so i wanted to try and find out how to seperate functions into different files and have them in a folder to only load when needed to save on resources and easy for me to manage and add too.

i have looked at the said page before i posted on here, but i cant see or understand how i would use the autoloader to load all the different classes from each file and from what i can see it, they only show 1 in the examples (if this is the wrong way pplease tell me). which when i looked at a article it said this is the best way of doing things. i have googled it looked at multiple websites etc etc but have not had any explanation on how best to do this. so i was hoping i would of more luck here.

thanks again.

If you're using classes simply to group functions by theme, you're not doing OOP.  You're only adding unneeded complexity to what could easily be solved simply by having pure function library files.

 

OOP is something that should not be attempted until you have a firm grasp of the fundamentals.  Otherwise you'll just end up confusing yourself.

 

If you insist on using spl_autoload_register, here's how you'd do it:

 

function myLoader($className)
{
   include_once("classes/" . $className . ".php");
}

spl_autoload_register("myLoader");

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.