Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End How to best manage large PHP classes

  • This topic is empty.
Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #45871
    fooman
    Participant

    I 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 :)

    #140728
    Paulie_D
    Member

    Yes 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?

    #140733
    TheDoc
    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 ;)

    #140747
    Paulie_D
    Member

    Oops…I never look at the Sections.

    My error.

    #140749
    TheDoc
    Member

    cc @traq

    #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.

    #140814
    Alen
    Participant
    #140815
    __
    Participant

    >Single_responsibility_principle

    yesyesyes, that too. : )

    #140850
    fooman
    Participant

    Thanks 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();
    }
    }
    }
    #140851
    fooman
    Participant

    With 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?

    #140857
    unasAquila
    Participant
    #140861
    fooman
    Participant

    interfacing is new to me. Thx!

    #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 );
    }
    }

    #143575
    saeed55sd
    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

Viewing 14 posts - 1 through 14 (of 14 total)
  • The forum ‘Back End’ is closed to new topics and replies.