montyonthebonty Posted February 25 Share Posted February 25 I have been using PHP Classes for a while now, after watching a few classes on the web. They all generally follow the same format, which is either: class findDepotsController extends findDepots { private $depotCode; public function __construct($depotCode) { $this->depotCode = $depotCode; } public function checkDepots() { $depotList = $this->searchDepots($this->depotCode); return $depotList; } } or if there are no variables: class systemUpdatesController extends systemUpdates { public function getSystemUpdates() { $updateList = $this->listUpdates(); return $updateList; } } Is there a good way of doing this for something where some of the methods require parameters and some of them don't. For example, I want a featureRequests class, with a method for adding a feature request, which will require details and a method for simply listing them, which doesn't. If I add parameters to the constructor then the list one will need a load of null parameters passing in, which seems kind of messy. Thanks Chris Quote Link to comment https://forums.phpfreaks.com/topic/326869-constructing-php-classes/ Share on other sites More sharing options...
maxxd Posted February 25 Share Posted February 25 Class methods do not have to match the constructor's signature - each method can require as many or as few properties as it needs. For instance, given your example, this is perfectly acceptable: <?php class FeatureRequests { private $details = []; public function __construct(array $details) { $this->details = $details; } public function getAllDetails() { return $this->details; } public function getDetail($index) { if(isset($this->details[$detail])){ return $this->details[$detail]; }else{ return 'Index not set'; } } } In this case, the constructor requires an array, getDetail() requires a string, and getAllDetails() doesn't require any parameters. What matter is that when you invoke the object method, you pass it the parameters it needs. Quote Link to comment https://forums.phpfreaks.com/topic/326869-constructing-php-classes/#findComment-1650475 Share on other sites More sharing options...
gizmola Posted February 25 Share Posted February 25 As maxxd illustrated, the important thing to understand is that every class has a constructor. Parameters that are essential to the creation of the object need to be defined in the class constructor. If you don't provide a class constructor, a default one is called. This is typical across Oop languages. So you want to think about what data the class will need to fulfill it's goals, and require those as parameters in the constructor. You don't call the constructor directly, it is called when you create an object using new: $someVariable = 88; $someArray = ['monday', 'wednesday']; $dtObj = new DateTime(); $myObj = new MyClass('Some Data', $someVariable, $someArray, $dtObj); $report = $myObj->getReport(); As an example here, you would have to assume that the MyClass constructor MyClass::__construct(....) would accept at least 4 parameters, based on this example, and would have a need and reason for these parameters. Frequently the only thing the constructor is going to do is assign those parameters to class variables defined in the class. Maxxd's example code illustrates this perfectly. The important thing to consider when designing the class methods is what the "interface" is for talking to it by calling methods. With PHP you can formalize this idea with a separate interface definition, but I'm not going to go into why you would use interfaces or not. All I want to clarify is that you can think of the interface to your object as the list of PUBLIC methods you defined in the class. In your code, anytime you will want to directly call a method, that method needs to be defined as public. Certain magic methods like the __construct() method are required to be public. But you can also have methods that are only used within class code. Those methods should be either private or protected. In most cases you will want to start by making those internal methods private. You also want your class variables to be private (again see Maxxd's example class!, notice that he defined the one class variable $details to be private. This means that a user can not do this: $details = ['Add Button', 'New List Screen']; $fr = new FeatureRequests($details); echo $fr->details[0]; Because the class $details variable was defined as private, you can't directly access it. This is a typical design choice, that facilitates the OOP principle of "information hiding" where the form of the internal data structure(s) of the class are hidden from code using that object. A user should not know nor depend on what the form of the details class variable is. The only thing that a user of a class should be interested in is the public methods the class provides. Quote Link to comment https://forums.phpfreaks.com/topic/326869-constructing-php-classes/#findComment-1650484 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.