Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8019273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:20:33+00:00 2026-06-04T21:20:33+00:00

I’ve been going procedural for a long time now, never making my own classes.

  • 0

I’ve been going procedural for a long time now, never making my own classes. Made my own functions but never objects. Classes are so confusing to me, I’m lost in my code now. I don’t understand how they are supposed to make things easier and more efficient. Could someone maybe point me in the right direction of what I’m doing wrong based on how I’m coding here?

Working on the previous example, I’m making a box and few things to throw in it. Books. Books have different dimensional ways to arrange putting them in the box, so I want whatever the way to align it when it’s put in to determine how many you can fit in there. LimitDeterminer determines how many can fit if they’re all put in the same orientation, and then will compare against what’s already in there. Just a briefing, and the code is not complete because I’m frustrated and confused which brings me here to correct my mistakes. I also think I’m doing something wrong as I constantly have to specify the objects to my functions…

Thanks

<?php

function feetToInches($feet){ //Converts feet to inches
    //$feet = $feet * 12;
    return $feet;
}



class Book{
    var $l = 7;
    var $w = 5;
    var $h = 1;
    var $name = "Book";

    function getDimensions($x){ //Return specified dimension, input as string.
        $dimensionArray = array(
                                "l" => $this->l,
                                "w" => $this->w,
                                "h" => $this->h
                                );
        return $dimensionArray[$x];
    }
}

class Box{
    //This is a box. It has length, width, and height, and you can put things in it.
    var $length = 0;
    var $width = 0;
    var $height = 0;
    var $storedArray = array();
    var $totalPerms = array();

    //Set length,width,height
    function setDimensions($l, $w, $h){
        $this->length = feetToInches($l);
        $this->width = feetToInches($w);
        $this->height = feetToInches($h);
    }



    function storedPerms($array){
        $this->totalPerms[] = $array;
        //results in order
        //lhw
        //hlw
        //wlh
        //whl
        //hwl
        //lwh
    }


    function storeThings($thing, $way){
        $ori = $this->totalPerms[$way];
        var_dump($ori);
        $this->storedArray[] = $thing;
        $this->CalcArrange($ori[0],$ori[1],$ori[2],$thing);
    }

    function getThings($key = null) {
        if ($key) {
            return isset($this->storedArray[$key]) ? $this->storedArray[$key] : null;
        } else {
            return $this->storedArray;
        }
    }

    //Set what's against box length to x, width to y, and height to z. Possible values are "l","w","h".
    function CalcArrange($x,$y,$z,$Thing){
        $lengthDiv = 0;
        $widthDiv = 0;
        $heightDiv = 0;


        $lengthDiv = $this->length / $Thing->getDimensions($x); // 24/7   5    5     7      
        $widthDiv = $this->width / $Thing->getDimensions($y);  //14/5     7    1      1
        $heightDiv = $this->height / $Thing->getDimensions($z); //10/1    1    7      5

        $lengthDiv = intval($lengthDiv);
        $widthDiv = intval($widthDiv);
        $heightDiv = intval($heightDiv);

        echo "<br />" . "Length " . $lengthDiv . " times " . "width " . $widthDiv . " times" . " height " . $heightDiv . " is " . ($lengthDiv * $widthDiv * $heightDiv) . "<br />";
    }



}



$thatBook = new Book;
$BookBox = new Box;
$amount = 96;

$BookBox->setDimensions(24,14,10);



//Put objects in box
function putInBox($obj, $box){
    $box->storeThings($obj, 3);
}



$books = $BookBox->getThings();



foreach ($books as $v){
    echo $v->name . "<br />";
}

function CalcTotalLWH($books){
    foreach($books as $book){
        $lengthSum += $book->l;
        $widthSum += $book->w;
        $heightSum += $book->h;
    }


    echo $lengthSum . "<br />";
    echo $widthSum . "<br />";
    echo $heightSum . "<br />";
    //echo $BookBox->length;

}



echo "<br />";


/*
for($i=0; $i < $possible; $i++){
    $addVal = array_pop($dimArray);
    array_unshift($dimArray, $addVal);
    //var_dump($dimArray);
    $BookBox->CalcArrange($dimArray[0],$dimArray[1],$dimArray[2], $thatBook);
}
*/



function findPerm($array, $map){
    $tmp = $array[$map[2]];
    $array[$map[2]] = $array[$map[1]];
    $array[$map[1]] = $tmp;
    return $array;
 //012   
}

function echoAllArray($array){
    echo "Now trying with ";
    foreach($array as $v){
        echo $v . " ";
    }
}

//Determine possible limits of Box to Object
function LimitDetermine($Obj, $Box){
    $dimArray = array("l","w","h");
    $possible = (count($dimArray)) * 2; //for possibilities. DOING IT RONG USE FACTORIALS.
    $map = array(0,1,2);

    foreach($dimArray as $k => $try){

        //012
        //021
        for($i=0; $i < 2; $i++){
            $dimArray = findPerm($dimArray,$map);
            echoAllArray($dimArray);
            $Box->CalcArrange($dimArray[0],$dimArray[1],$dimArray[2], $Obj);
            $Box->storedPerms($dimArray);
            $addVal = array_pop($map);
            array_unshift($map, $addVal);
        }

        //120
        //102

        //201
        //210
    }
}

LimitDetermine($thatBook,$BookBox);

putInBox($thatBook, $BookBox);

/*
$BookBox->CalcArrange("l","w","h", $thatBook); //Face up flat length to length 60
$BookBox->CalcArrange("w","l","h", $thatBook); //Face up flat width to length 80
$BookBox->CalcArrange("w","h","l", $thatBook); //Width to length, standing. 56
$BookBox->CalcArrange("l","h","w", $thatBook); //The way I usually do it. 84
$BookBox->CalcArrange("h","w","l", $thatBook);
*/

var_dump($BookBox->totalPerms);
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:20:35+00:00Added an answer on June 4, 2026 at 9:20 pm

    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.

    For example, a box can storeThings and you can setDimensions of a box. You can even getThings inside a box, but storedPerms doesn’t make much sense, nor does CalcArrange. I’ll get to those a little later.


    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.

    class Box {
    
        // Define dimension attributes
        public $length;
        public $width;
        public $height;
        public $books;
    
        public function __construct( $length=0, $width=0, $height=0 ) {
            $this->length = feetToInches( $length );
            $this->width =  feetToInches( $width );
            $this->height = feetToInches( $height );
        }
    
    }
    

    And for book…

    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->length = feetToInches( $length );
            $this->width =  feetToInches( $width );
            $this->height = feetToInches( $height );
            $this->name = $name;
        }
    
    }
    

    This lets you create books and boxes like so:

    $myNewBox = new Box(60, 12, 24);
    $myNewBook = new Book('All Quiet on the Western Front', 12, 4, 8);
    

    From here, you could probably use something like:

    class Box {
    
        ...
    
        public function getCapacity( $book ) {
    
            // Determine how many full books can fit within each dimension
            $lengthCapacity = floor( $this->length / $book->length );      
            $widthDiv =       floor( $this->width / $book->width );
            $heightDiv =      floor( $this->height / $book->height );
    
            return $lengthCapacity * $widthCapacity * $heightCapacity;
        }
    }
    

    And then, you could run the thing by saying:

    // How many of my new book can fit in my new box?
    $bookCapacity = $myNewBox->getCapacity( $myNewBook );
    

    If you wanted to try out rotating the book in different ways to determine the absolute maximum capacity, do just that!

    class Book {
    
        ...
    
        public function rotate( $axis ) {
    
            $startingLength = $this->length;
            $startingWidth = $this->width;
            $startingHeight = $this->height;
    
            if( $axis == 'x' ) {
                $this->height = $startingLength;
                $this->length = $startingHeight;
    
            } elseif( $axis == 'y' ) {
                $this->width = $startingLength;
                $this->length = $startingWidth;
    
            } elseif( $axis == 'z' ) {
                $this->width = $startingHeight;
                $this->height = $startingWidth;
    
            }
    
        }
    }
    

    Then you can write a variation of your getCapacity function to utilize that…

    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->rotate($axisToRotate);
                $maxCapacity = max( $maxCapacity, $this->getCapacity( $book ) );
            }
    
        }
    

    And voila! Your new function (assuming I’ve dotted my I’s and crossed my Ts correctly), can be run like so:

    $absoluteMaxCapacity = $myNewBox->getMaxCapacity( $myNewBook );
    

    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.

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.