Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Object-oriented code is a very different way of thinking to procedural, I'm sure you familiar with that already. In essence, the idea behind 'true' OO (and this is mainly my interpretation here) is that each object that you create 'does' things, and 'knows' about things. Often, you can determine whether you're going about things in a logical, understandable way by whether or not that kind of object would actually know or do the things you're requesting of it.</p> <p>For example, a <strong>box</strong> can <em>storeThings</em> and you can <em>setDimensions</em> of a <strong>box</strong>. You can even <em>getThings</em> inside a <strong>box</strong>, but <em>storedPerms</em> doesn't make much sense, nor does <em>CalcArrange</em>. I'll get to those a little later.</p> <hr> <p>You can use the __construct function to make your life a little easier. Whenever you create a Book or Box, you can set its initial dimensions/configuration without having to call a secondary function.</p> <pre><code>class Box { // Define dimension attributes public $length; public $width; public $height; public $books; public function __construct( $length=0, $width=0, $height=0 ) { $this-&gt;length = feetToInches( $length ); $this-&gt;width = feetToInches( $width ); $this-&gt;height = feetToInches( $height ); } } </code></pre> <p>And for book...</p> <pre><code>class Book { // Define dimension attributes public $length; public $width; public $height; public $name; public function __construct( $name='Book', $length=0, $width=0, $height=0 ) { $this-&gt;length = feetToInches( $length ); $this-&gt;width = feetToInches( $width ); $this-&gt;height = feetToInches( $height ); $this-&gt;name = $name; } } </code></pre> <p>This lets you create books and boxes like so:</p> <pre><code>$myNewBox = new Box(60, 12, 24); $myNewBook = new Book('All Quiet on the Western Front', 12, 4, 8); </code></pre> <p>From here, you could probably use something like:</p> <pre><code>class Box { ... public function getCapacity( $book ) { // Determine how many full books can fit within each dimension $lengthCapacity = floor( $this-&gt;length / $book-&gt;length ); $widthDiv = floor( $this-&gt;width / $book-&gt;width ); $heightDiv = floor( $this-&gt;height / $book-&gt;height ); return $lengthCapacity * $widthCapacity * $heightCapacity; } } </code></pre> <p>And then, you could run the thing by saying:</p> <pre><code>// How many of my new book can fit in my new box? $bookCapacity = $myNewBox-&gt;getCapacity( $myNewBook ); </code></pre> <p>If you wanted to try out rotating the book in different ways to determine the absolute maximum capacity, do just that!</p> <pre><code>class Book { ... public function rotate( $axis ) { $startingLength = $this-&gt;length; $startingWidth = $this-&gt;width; $startingHeight = $this-&gt;height; if( $axis == 'x' ) { $this-&gt;height = $startingLength; $this-&gt;length = $startingHeight; } elseif( $axis == 'y' ) { $this-&gt;width = $startingLength; $this-&gt;length = $startingWidth; } elseif( $axis == 'z' ) { $this-&gt;width = $startingHeight; $this-&gt;height = $startingWidth; } } } </code></pre> <hr> <p>Then you can write a variation of your getCapacity function to utilize that...</p> <pre><code>class Box { ... public function getMaxCapacity( $book ) { // There are 6 unique ways a book can be positioned $axesToRotate = array( 'x', 'y', 'z', 'x', 'y', 'z' ); $maxCapacity = 0; foreach( $axesToRotate as $axisToRotate ) { $book-&gt;rotate($axisToRotate); $maxCapacity = max( $maxCapacity, $this-&gt;getCapacity( $book ) ); } } </code></pre> <p>And voila! Your new function (assuming I've dotted my I's and crossed my Ts correctly), can be run like so:</p> <pre><code>$absoluteMaxCapacity = $myNewBox-&gt;getMaxCapacity( $myNewBook ); </code></pre> <hr> <p>In closing, semantics is absolutely amazingly important here. I spend about 10% of my time coding thinking about the best thing to call a method or a variable. Also, avoid code duplication where possible. In the example above, we implemented the getCapacity function which was then later utilised by getMaxCapacity to save time and simplify our getMaxCapacity function.</p> <p>Hope that helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload