MasterACE14 Posted June 1, 2011 Share Posted June 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/238083-structuring-my-class/ Share on other sites More sharing options...
trq Posted June 1, 2011 Share Posted June 1, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/238083-structuring-my-class/#findComment-1223445 Share on other sites More sharing options...
MasterACE14 Posted June 1, 2011 Author Share Posted June 1, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/238083-structuring-my-class/#findComment-1223534 Share on other sites More sharing options...
KevinM1 Posted June 1, 2011 Share Posted June 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238083-structuring-my-class/#findComment-1223537 Share on other sites More sharing options...
ignace Posted June 1, 2011 Share Posted June 1, 2011 Post the class in it's entirety then we can advise you on structure, possible patterns (Strategy maybe?) and such. But with fake code we can only point out syntax error's. Quote Link to comment https://forums.phpfreaks.com/topic/238083-structuring-my-class/#findComment-1223611 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.