PHP design issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mdk999
Forum Newbie
Posts: 22
Joined: Fri May 08, 2009 3:21 pm

PHP design issue

Post by mdk999 »

Hi,

I am new to the forum and well have some questions I hope someone can answer..

1. Classes - static vs instantiated

I've written an app which uses abstract classes (by design) and I also implemented an autoloading of the the classes, is there anything inherently 'bad' or 'wrong' in doing that?

2. Program specifc variables and constants

Within said program I have config options ie folder locations, filenames, etc. I am storing these variables in $_SESSIONS['config'], is there a better way of doing this ..?

3. Registry

A registry class is being used to access the previously mentioned configs in that none of the other functions/methods accesses this data directly.. bad/good?

4. Class within a class

I have the need to parse files, I'd like to be able to load multiple files to be parsed.. my thought was to create a class to handle all the manipulation this was my idea ..

Code: Select all

 
abstract class loadfiles{
 
static $Loaded;
 
function helper($handle){
 
if(!is_object(self::$Loaded[$handle])){
 
include('helper_class.php');
 
return new helper();
 
} else return self::$Loaded[$handle];
 
}
 
function load_handle($files){
 
if(!is_array($files) return;
 
foreach($files as $k){
 
 self::$Loaded[$k] = self::helper($k);
}
 
}
 
}
 
 
The problem I have is when I do a var_dump() on $Loaded the loaded objects shows empty. Maybe my approach to this is wrong .. thoughts and comments are welcomed
Last edited by Benjamin on Fri May 08, 2009 4:22 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP design issue

Post by Christopher »

1. I would recommend instantiated unless you cannot find a good design without using statics.

2. Probably a Config object would be better. The session is for storing data that only lasts for the duration of a user session.

3. A Registry is a place to find commonly needed objects. If you need that then a Registry is a good solution.

4. I'm not sure you need a static in that class, but you do need to assign the helper object you create to a property -- then return the property.
(#10850)
mdk999
Forum Newbie
Posts: 22
Joined: Fri May 08, 2009 3:21 pm

Re: PHP design issue

Post by mdk999 »

Thanks so much for the quick response...

1. I was trying to cut down on the overhead associated with an instantiated class. I also dont want to make the class objects global (which is something I am trying to avoid) or having to include the class files in the script. I thought about a singleton but I would still need to do something like .

Code: Select all

 
 
function get_user($userid){
global $db; //trying to avoid this
 
$sql = "select * from users where `id` = '{$userid}'";
 
return $db->fetch($db->query($sql));
 
}
 
2. So something like ..

Code: Select all

 
$config->get('home'); //this can say get 'home' from say a database?
$config->set('home','data/user'); //sets home in the database
 
I was trying to avoid having to query the database everytime I need to check for or use a config option..

3. I just did a quick test using my code and inline classes (class definitions in the same file), iit seems to work as expected.. maybe something is going on with included classes ..?
Last edited by mdk999 on Fri May 08, 2009 5:16 pm, edited 2 times in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP design issue

Post by Christopher »

A Gateway style class would look like this:

Code: Select all

class User {
  protected $db;
 
  function __construct($db) {
     $this->db = $db;
  } 
 
  function find($userid){
     $sql = "select * from users where `id` = '" . $this->db->escape($userid) . "'";
 
     return $this->db->fetch($this->db->query($sql));
  }
 
}
(#10850)
mdk999
Forum Newbie
Posts: 22
Joined: Fri May 08, 2009 3:21 pm

Re: PHP design issue

Post by mdk999 »

I understand your example $db would need to be passed to the class when instantiated.. Wouldn't every class that needs database access need to be passed the $db object? $db would now be global to the entire class. It seems a bit inefficient (to me) to do that when there could be a .. 5 other classes all of which would need to add that as a param in the class setup. When you then have classes interacting with other classes ie. user class checking the security class if a can access a certain file, etc How would you handle that in this structure? You'd have to pass the object to that method or again make it global to the class. In the end you'd have references of all the classes in each class, which (again to me) seems wrong :) Am I looking at this incorrectly?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP design issue

Post by Christopher »

You'd have to show me an example of a "user class checking the security class if a can access a certain file" for me to know what you mean. In general you want classes that are small and focused on a single task. That means passing some object to other objects -- which makes the dependencies clear.
(#10850)
mdk999
Forum Newbie
Posts: 22
Joined: Fri May 08, 2009 3:21 pm

Re: PHP design issue

Post by mdk999 »

Sure here is an example ..

Code: Select all

 
 
class config {
 
//accesses config options
 
//gets a config option
function get_config($option=false){
 
}
 
 
//sets a config option
function set_config($field,$value){
 
}
 
}
 
class db{
 
//connects to the db
function connect(){
 
}
 
//retrieves rows
function fetch($resource){
 
}
 
//query the db
function query($sql){
 
}
 
}
 
class security {
 
//checks if a user is logged in
function is_loggedin($user){
 
}
 
//checks is user is allowed to for instance save pictures
function allowed($user){
 
}
 
}
 
class user{
 
//gets all the users information
function get_userinfo($userid){
 
}
 
//saves a users picture to the file system
function save_userpic($userid,$picture){
 
}
 
}
 
 
As you can see all the classes above do different things .. So say you want a user to be able to save a picture. The user class needs to access the security class to see if the user is logged in and is allowed to save a picture. The security class needs to access the database to validate the user information. If all is well the save_userpic method now needs the file locations from the config class to store the pic and link it to the user. All these classes need to access the database, the user class also needs to access the security class to validate a user, etc. How would I get all these talking without my previously mentioned issue with passing the $db to all classes, adding classes into each other, making them global or global to each script. - Thank
Last edited by Benjamin on Fri May 08, 2009 6:58 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply