Jump to content

Recommended Posts

Good Afternoon,

 

I'm having trouble working out how to structure my class, I don't think how it currently is structured is as efficient as it could be. It does work how it currently is, but probably is not the most ideal way to go about doing things?

 

The class goes something like this...

class Name {
     public $x;
     public $a1, $a2, $a3, $a4;

     __construct($x=array(1)) {
         $this->x = ValidateArray($x);
         $a1 = self::Method1($this->x);
         $a2 = self::Method2($this->x);
         $a3 = self::Method3($this->x);
         $a4 = self::Method4($this->x);
} 

    private ValidateArray($x) {
    // validate the array
    return $this->x;
}

    public Method1($x) {
    $x = ValidateArray($x);
     // do stuff
    return $this->a1;   
}   

    public Method2($x) {
    $x = ValidateArray($x);
     // do stuff
    return $this->a2;   
}   

    public Method3($x) {
    $x = ValidateArray($x);
     // do stuff
    return $this->a3;   
}   

    public Method4($x) {
    $x = ValidateArray($x);
     // do stuff
    return $this->a4;   
}   

}

 

  • I'm sure there must be an easier way to use ValidateArray(); in each method?
  • If I want to call a public method on it's own, is there a way to cancel the __construct() ? seems inefficient for the __construct() to run all the methods if I'm not using the instantiation to pass an array as an argument, but rather just want to use a particular method?

 

Thank you in advance for any help/suggestions and insight.

 

Kind Regards,

Ace

Link to comment
https://forums.phpfreaks.com/topic/238083-structuring-my-class/
Share on other sites

It does work how it currently is

 

I very much doubt it, your missing the 'function' keyword before your __construct.

 

As for the structure... what exactly is this thing meant to do?

 

Yeah just noticed I was missing that, it's in the actual script... There's just far too much code to post so I've simplified it.

 

Each method performs a different math calculation. So an array of values can either be passed as an argument to the __Construct() and all methods are run using that array, or an array can be passed as an argument to a specific method if the user only wants the result of that particular method. I'm just not all that sure this is the best way to do it? If an array is passed to one of the methods, the default array(1) is still passed to the __construct() is that not a waste of resources?

 

Also is there a better way to call ValidateArray(); rather than copy pasting the function call in every method, meaning it will be called several times throughout the script if an array is passed to the __construct() , validating an array that has already been validated?

Each method performs a different math calculation. So an array of values can either be passed as an argument to the __Construct() and all methods are run using that array, or an array can be passed as an argument to a specific method if the user only wants the result of that particular method. I'm just not all that sure this is the best way to do it? If an array is passed to one of the methods, the default array(1) is still passed to the __construct() is that not a waste of resources?

 

For that to work, you'd need to make your methods static.

 

But, really, it sounds like you don't really understand why people use OOP, so you're trying it just because you heard it's the way to go.  You need to understand the fundamentals - what an object is, what constructors actually do, and how it all fits togehter - because right now you're writing a bunch of gibberish code.

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.