- This topic is empty.
-
AuthorPosts
-
June 27, 2013 at 12:02 pm #45871
fooman
ParticipantI have a class that is starting to become hard to manage because it has a lot of various methods and functions that are tough to keep organized (I’m doing my best). It’s not too crazy, but it’d be awesome if I could take these functions and group them in separate classes/files.
I’m not sure how to best show some code to explain this. I’d like to have one instantiation of a class from the webpage, and still have access to all these functions… yet not have all of them in a single class.
If I need to write out some code please ask, I’ll gladly try. I’m finding it hard to do so without confusing my actual question haha.
Thanks for any guidance :)
June 27, 2013 at 12:07 pm #140728Paulie_D
MemberYes some code would be useful.
Of course, classes don’t have ‘methods’ or ‘functions’ as we commonly understand them but without seeing what we are taking about it’s hard to go into greater detail.
Perhaps a Codepen?
June 27, 2013 at 12:26 pm #140733TheDoc
Member> Of course, classes don’t have ‘methods’ or ‘functions’ as we commonly understand them – @Paulie_D
Since we’re in the PHP section, I think he’s talking about a PHP Class, not CSS, if that’s what you were thinking ;)
June 27, 2013 at 1:57 pm #140747Paulie_D
MemberOops…I never look at the Sections.
My error.
June 27, 2013 at 2:04 pm #140749June 27, 2013 at 10:00 pm #140812__
Participant…aaAA**AA**bstraction
!
I know that sounds a little vague. I’d need to see your code to offer any specific advice. But in general, classes get “too big” because they try to do too much and/or are not well organized.
June 27, 2013 at 10:04 pm #140814June 27, 2013 at 10:05 pm #140815__
Participant>Single_responsibility_principle
yesyesyes, that too. : )
June 28, 2013 at 9:04 am #140850fooman
ParticipantThanks guys! I think that wikipedia page is a great concept that honestly I was not taught in school!
It’s hard to post much code because the code is tied to a project I’m not allowed to post about, whether or not anyone cares haha
I guess I will do some more research and try my hand at organizing my code in a more structured manner, rather than throwing it in a single file.
One question I do have is, how do you use properties of one class within another without having to pass a boatload of properties through function parameters? Also, how do you load other classes within each other so they can all communicate?
Terrible example:
class Person
{
private $name = "";
private $bday = "";
private $email = "";
public function __construct()
{
// Do something if needed
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
// .... Do the some sets and gets for other properties as needed
public function useEmail()
{
try{
$emailer = new Emailer();
$emailer->sendEmail($fname, $lname, $email);
}catch(Exception $e){
return $e->getMessage();
}
}
}June 28, 2013 at 9:06 am #140851fooman
ParticipantWith that, how do I include the Emailer class within the Person class properly, and what if I have functions that require passing a rather large number of properties? Do I just pass $this?
June 28, 2013 at 10:06 am #140857unasAquila
Participantclass [abstraction](http://php.net/manual/en/language.oop5.abstract.php “abstraction”) + [autoloading](http://www.php.net/manual/en/function.spl-autoload-register.php “autoloading”)
[interfaceing](http://www.php.net/manual/en/language.oop5.interfaces.php “interfaces”)June 28, 2013 at 10:56 am #140861fooman
Participantinterfacing is new to me. Thx!
June 28, 2013 at 3:42 pm #140878__
Participant[extend](http://php.net/extends) is your friend.
For example, I have a class that represents a generic data object – anything with property->value pairs, like an assoc. array, but it also handles validation, array-like access and methods, instance indexing, and loading/saving from a database. There’s a truckload of methods that it needs. To keep it manageable, I broke it into several classes that **extend** each other:
— the _modelable_ abstract class handles the data itself: how to create new objects, initialize their properties, and validate+assign values. Also handles things like [ArrayAccess](http://php.net/arrayaccess/).
— the _storable_ abstract class **extends** the modelable class, and has methods related to storage (typically a DB). It has an [interface](http://php.net/interface/) that tells what methods it needs from the implementing class – CRUD methods, basically, that are designed to access whatever storage (database) you want to use.
— the _queryable_ abstract class extends the storable class, and has methods that help you create objects from search results: you give it a map of properties you want, and it searches for them in the database – then creates the new object(s) from those records and hands them back.
— the _indexible_ class keeps track of objects you’ve already created from storage, so if you ask for the same object again, you can get the one you already created instead of connecting to the DB again.
*****
>how do I include the Emailer class within the Person class properly, and what if I have functions that require passing a rather large number of properties?like so:
$emailer = new myEmailClass();
$person = new myPersonClass( $emailer );
/* …snip… */
class person{protected $emailer;
public function __construct( myEmailClass $emailer ){
$this->emailer = $emailer;
/* your other code */
}public function sendEmail( $m ){
$this->emailer->mail( $m );
}
}July 19, 2013 at 3:37 am #143575saeed55sd
Member> extend is your friend.
yes
and if you want manage your classes I offer you to separate your classes and move to MVC methodology and layout
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.