Jump to content

loading self made objects in php


Xdega

Recommended Posts

so I am about to start on a small object oriented project, for practice/learning etc.

I have 5 files in a folder called "objects" : (obj1.php obj2.php obj3.php obj4.php obj5.php) .

I then created a "classloader.php" with the following code:

<?php
include "objects/obj1.php";
include "objects/obj2.php";
include "objects/obj3.php";
include "objects/obj4.php";
include "objects/obj5.php";
?>

 

and then finally my main page (for the sake of testing):

<!DOCTYPE HTML>
<head>
<title>...</title>
</head>
<?php include "classloader.php"; ?>
<body>
<header></header>
<nav></nav>
<section>
<?php 
object1();
object2();
object3();
object4();
object5();
?>
</section>
<footer></footer>
</body>

 

What is the best way to handle the loading of objects in php? I think I am doing it somewhat wrong. Anyone lend me some more info here?

thanks much.

 

 

Link to comment
https://forums.phpfreaks.com/topic/230677-loading-self-made-objects-in-php/
Share on other sites

so, I took a look at this.

Changed my classloader.php to

 

<?php
//DEFINE AUTOLOADER
function __autoload($className) { 
      if (file_exists($className . '.php')) { 
          require_once $className . '.php'; 
          return true; 
      } 
      return false; 
} 
?>

 

I also changed the file names/dir to be consistent with the above code.

Something like below:

 

index.php

--/objects/

----classloader.php

-----object1.php

-----object2.php

-----object3.php

-----object4.php

-----object5.php

 

for clarification, here is the relevant content in the index.php

 

 

<?php include "objects/classloader.php"; ?>

<?php 
object1();
object2();
object3();
object4();
object5();
?>

 

from the manual, it looked pretty straight forward, but doesn't seem to be working.

any ideas what I am doing wrong here?

Check your paths.

 

<?php
//DEFINE AUTOLOADER
function __autoload($className) { 
      if (file_exists('objects/'.$className . '.php')) { 
          require_once 'objects/'.$className . '.php'; 
          return true; 
      } 
      return false; 
} 
?>

Hmm.. Well using all of the input from all the users on this topic it works for me:

 

/index.php

<?php include "objects/classloader.php";

$obj1 = new object1();
$obj2 = new object2();
$obj3 = new object3();
?>

 

/objects/classloader.php

<?php
//DEFINE AUTOLOADER
function __autoload($className) { 
$file = 'objects/' . $className . '.php';
if (file_exists($file)) { 
	require_once $file; 
	return true; 
} 
return false; 
} 
?>

 

/objects/object1.php

<?php

class object1{

public function __construct(){
	echo(get_class($this));
}
}

?>

 

/objects/object2.php

<?php

class object2{

public function __construct(){
	echo(get_class($this));
}
}

?>

 

/objects/object3.php

<?php

class object3{

public function __construct(){
	echo(get_class($this));
}
}

?>

 

Does this work for you?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.