Jump to content

Feedback On This Oop Design


MySQL_Narb

Recommended Posts

I've been trying to make a more centralized OOP structure, where one class can ease communication between all other classes (server being the head/main class).

 

Any feedback on this?

 


<?php

class server{
   public function loadClass($class){
       if($this->$class == null){
           if(file_exists($class.'.php')){
               include($class.'.php');
               $this->$class = new $class();
           }else{
               $this->throwError('class '. $class.' does not exist');
           }
       }else{
           $this->throwError($class. 'already loaded.');
       }
   }

   public function dropClass($class){
       $this->$class = null;
   }

   public function runMethod($class, $method, array $params = array()){
       if($this->isLoaded($class)){
           if(count($params) != 0){
               $par = '';
               $i = 1;
               foreach($params as $key => $value){
                   $par .= ($i == count($params)) ? $method : $method.',';
                   $i++;
               }
           }
           $this->$class->$method(($par != null) ? $par : null);
       }else{
           $this->throwError($class .' is not loaded.');
       }
   }

   protected function isLoaded($class){
       return ($this->$class == null) ? false : true;
   }

   protected function throwError($error){
       die('<font color="red">'. $error .'</font>');
   }
}

?>

 

Example of usage


<?php

require('server.php');
$parent = new server();

$parent->loadClass('base');
$parent->runMethod('base', 'hey');

?>

Edited by MySQL_Narb
Link to comment
Share on other sites

I think the intent is to make seperate class files and make it so that the server will load the file when the class is called rather then needing to require or include this will do it dynamically.

 

IE: you will only need to load server.php in the scope of the program

Now I want to call class mysql

server.php will see if there is a mysql in current existance if not it will try loading mysql.php and creating a class from that.

 

At least this is what I gathered from my brief reading of the code

Interesting concept but not sure if I would actually use it, and if I did I would probably mod it to just auto link and return the class I'm planning on using.

Link to comment
Share on other sites

Good point: if this class is meant to solve the problem of autoloading classes, PHP already supports that out-of-the-box (but please use the SPL function, not __autoload()).

Now that is something I might actually use since I normally structure my programs in the same way; as in I usually store classes in the same subfolder with the same basic extension, although I have not got into the habit of naming my files directly after the class.

IE: my database class is usually db.**class_extention** not database.**class_extention**

Link to comment
Share on other sites

I think the intent is to make seperate class files and make it so that the server will load the file when the class is called rather then needing to require or include this will do it dynamically.

 

IE: you will only need to load server.php in the scope of the program

Now I want to call class mysql

server.php will see if there is a mysql in current existance if not it will try loading mysql.php and creating a class from that.

 

At least this is what I gathered from my brief reading of the code

Interesting concept but not sure if I would actually use it, and if I did I would probably mod it to just auto link and return the class I'm planning on using.

 

The exact reasoning behind this. Easier control among everything.

Link to comment
Share on other sites

The exact reasoning behind this. Easier control among everything.

Yeah but you should really do some research in requinix's post about spl_autoload() functionality in PHP (5+) it already provides the basic functionality you are trying to code and does it in a more pleasant way by being able to return the class into an object variable rather than trying to run one class through another, (the only thing I would have modded in your original writeup.)

Link to comment
Share on other sites

spl_autoload(function($className)
{
   $className = ltrim($className, '\\');
   $fileName  = '';
   $namespace = '';
   if ($lastNsPos = strripos($className, '\\')) {
       $namespace = substr($className, 0, $lastNsPos);
       $className = substr($className, $lastNsPos + 1);
       $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
   }
   $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

   require $fileName;
});

 

This is what I would use in small scripts. Any other I use Composer and it solves the autoloading for me.

 

 

"autoload":{
 "psr-0":{
   "mylib": "src/"
 }
}

require 'vendor/autoload.php';

Edited by ignace
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.