Jump to content

Single Class Across Multiple Files


Garethp

Recommended Posts

So I had this idea for my classes when I was going to sleep to make it easier to edit and add to them. I really want to have my functions in a class over multiple files. I would like something like this

 

<?php
if(!isset($_GET['class']))
{
exit();
}

$class = $_GET['class'];

class $class 
{
include "$class/Login.php";
include "$class/Logout.php";
}
?>

 

But with a glob(); on the directory. However when I tried something like that as a test, it didn't work.

 

Can someone give me an example of how I would do this?

Link to comment
Share on other sites

I just like the idea behind it. Make my code more readable and accessible. Doesn't have anything to do with size. My User class for instance only has about 10 functions (for now) (I think). So there's no way to do it? Not even have, say, a chain of extends that has the extension class names for the previous class defined by a variable?

Link to comment
Share on other sites

So there's no way to do it?

 

None that would be particularly efficient or well designed, its not part of the language. The classkit extension may be of use, but it will make your scripts unportable across servers.

 

Not even have, say, a chain of extends that has the extension class names for the previous class defined by a variable?

 

That sounds like a terrible idea.

Link to comment
Share on other sites

It sounds like you're wanting to do too much with a single class.  Can you give a  concrete example of a class that you have which you think would benefit from being spread across multiple files, and explain why you think so?

Link to comment
Share on other sites

Honestly? I think a user management class would benefit from it. My user management class has the following functions

 

Restrict

Login

Get Info

Edit User

Calculate Age

__construct

 

And it already spans 400 lines.

 

And ultimately I also want it to have

 

Send PM

Get PMs

Favorite

Favorites

Follow

Add Contact

Submit Story

Forum Login

Chat Login

 

And many more. As it is it's a pain in the ass to go over my functions when I think that they need adjusting, and I figure it would be so much easier to read, edit and add functions if they were all in separate files under one folder called "User". I really dislike having a script as long as it is, especially when it could get MUCH longer

Link to comment
Share on other sites

It looks like you're not really grasping the concept of OOP clearly at all, instead you're essentially wrapping a bunch of procedural behaviour (discrete functions) within a class.  Those different parts that you mention should belong in very different, separate classes; just to give a couple of examples, logging in is all about authentication whereas restriction is about authorization -- two separate entities right there! Getting/sending PMs is entirely unrelated to retrieving a user's age, so should definitely not belong in the same class.  Etc..

Link to comment
Share on other sites

I just thought that OOP was a convenient way to store and use functions that relate to a certain element. What's the concept of OOP then?

 

A class defines the characteristics and behaviors of a single thing (an object). You are inadvertently jamming unrelated functionality within this object.

 

OOP is not something that is going to be explained in a single post, maybe you should invest in a good book on the subject?

Link to comment
Share on other sites

Ok, I know you guys said it was a bad idea, but I was too curious, because I knew there had to be a way to do it, and there was. Here' what I have

 

<?php

class User
{
public function __call($method, $args) 
	{
	if (isset($this->$method) === true) 
		{
		if(isset($args[0]))
			{
			$args = $args[0];
			$func = $this->$method;
			$func($args); 
			}
		else
			{
			$func = $this->$method;
			$func(); 
			}

		} 
	}
}

?>

 

And so I can use

 

$User->Login = function()
{
//stuff
}

 

And even have one parameter. That's all cool and works. But when I have ($param1, $param2) I have no idea how to make it work. I usually use associative arrays instead of set parameters anyway, so it's no big deal, but I'm just curious as to how I would get it to work

Link to comment
Share on other sites

If you really wish to go in this direction, you can use call_user_func_array

 

This undoubtly an interesting approach and I'm sure you've got a lot of fun developing it. Just be aware that it's breaking many of well established rules of OOP like:

 

http://en.wikipedia.org/wiki/Single_responsibility_principle

http://en.wikipedia.org/wiki/Interface_segregation_principle

http://en.wikipedia.org/wiki/Don%27t_repeat_yourself

Link to comment
Share on other sites

I'm not sure it can be breaking that many rules as languages that are better suited to OOP then php (eg Ruby and Python) allow similar approaches within there design.

 

PHP however doesn't, and I'm really not that sure the approach you are taking with php is particularly well thought out, designed, or what that feature was provided to actually do.

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.