masterlayouts Posted March 5, 2012 Share Posted March 5, 2012 Explaining classes you're saying that it is not necessary a better approach or superior approach, but a different approach. Depending of the situation it may be better to use classes or not. From a developer point of view makes sense to use classes as chances are will diminuish the amount of code to write in the future, but from the client point of view it seems to me that most of the time it is better to avoid using classes. It makes little sense to create a blueprint than instantiate your application as a particular case of the class. For example when using the database connector, it is known that the mysql connector is faster than the abstracted PDO driver. If you know from beginning that you will use mysql what is the point of using PDO. Isn't this a diservice for the client? If we're not talking about large projects or even if they are large, approaching class style doesn't constitues a disservice to the client? I understand the advantages for the programmer, however if you were the clients what would you say? Link to comment Share on other sites More sharing options...
Jonathon Posted March 5, 2012 Share Posted March 5, 2012 Most clients won't understand rhe difference and probably won't care. OOP is about re-usable code, so I would suggest that you don't make OO classes just for one project and then try to re-use them. I'd make a series of classes as a framework and then use them in projects if appropriate - then you could argue it is a service to the client as you've already done the hard work, all you're doing is passing details to the objects. 1 Link to comment Share on other sites More sharing options...
Antonio Conte Posted March 7, 2012 Share Posted March 7, 2012 I would say you should really try to code functional classes either way. I tend to ignore procedural code because I also code a lot of Java, which cannot be coded procedural like PHP. You will often code several functions to handle your code anyway. By creating classes, you will allow a clear cut use of your method. Because you can spesify private and protected methods in a class, you can program with a divide and concur mindset and code to a top-down approach instead of bottom-up. This is often the difference between those who have written a lot of code object-oriented and those who code procedural. As you become a better coder, you'll start to isolate the common problem areas in your code. PHP developers don't have to worry about problem such a arrayOutOfBoundsExceptions and such, but often you'll find similar problems you would easily need helper methods for. With all good programming, you'll tend to see a much more clear language in code, namly spoken language. This is an example from Java validating array positions in a multidimensional array, but it applies to PHP to. I've rewritten it to PHP here: // Class something public function addCell($row, $col) { // Validate array position if ( ! $this->validKey($row, $col) ) return; // If position is empty, add Cell object if( $this->emptyPos($row, $col) ) { $this->add($row, $col); } // If position exist and drawing mode is by click, remove object else if ( $isDrawnByClick && ! $this->emptyPos($row, $col) ) { // Cell exists in array. Remove it $this->remove($col, $row) } } private function add($row, $col) { // If array position is empty, add object if ( $this->emptyPos($row, $col) ) { $this->collection[$row][$col] = new Cell($row, $col); } } private function emptyPos($row, $col) { // Check if key is valid. Return true on empty position. False if set. if ( $this->validKey($row, $col) ) { return ( $this->collection[$row][$col] == null) ? true : false; } } private function validKey($row, $col) { return ( (0 <= $row && 0 <= $col) && ($this->numOfRows < $row && $this->numOfCols < $col) ) ? true : false; } This code is simply checking whether a clicked coordinate in a grid system is inside the grid. It's fairly simple programming. Because I've added helper methods such as emptyPos() (who also uses validKey()) to check array positions, I do not need to check input. All I have to do to alter this programs allowed array positions, is to change the objects member numOfRows and numOfCols. If I coded this in PHP, I would only have to add a SINGLE method to determine whether the input is a valid integer. (Java is stronly typed.) This illustrates the power of object orientation. Sure you could just as simply create these functions inside procedural code. The problem arrives when you need this functionality in another script. Sure, you could include it into your scripts. Another problem arise when you need small alterations for different uses. You'll then have to create several different functions to handle these alterations. Which of these should another programmer use? Without possibility to add viability level to a method, all kinds of functions are then available to the new coder. It just starts to get chaotic. Someone using your code, including yourself, will be very happy to be able to do something like this: $collection = new Collection(30, 50); $collection->addClick(10, 15); // Valid click. Cell object added to array $collection->addClick(311, 51); // NOT valid click. Everything still works as expected. Object not added Starts writing $collection->...(Looks in IDE. Programmer sees the removeClick() method) $collection->removeClick(10, 15); // First Cell object removed I'm not saying procedural code is wrong. In most cases though, you'll see most programming tasks has been done before. No need to re-invent the wheel each time. As you build classes, more and more code can be re-used. You'll become a better programmer, more in tune with the professional world, and will write more clear-cut code. The transition to other languages is also a lot smaller. Start coding object-oriented. You'll really see the advantages in the long run. 1 Link to comment Share on other sites More sharing options...
Recommended Posts