1

私は長い間プロシージャルに取り組んできましたが、独自のクラスを作成したことはありません。独自の機能を作成しましたが、オブジェクトは作成しませんでした。クラスは私にとって非常に紛らわしいので、今は自分のコードで迷っています。物事をより簡単かつ効率的にする方法がわかりません。ここでのコーディング方法に基づいて、誰かが私が間違っていることの正しい方向に私を向けることができますか?

前の例に取り組んで、私は箱とそれに入れるいくつかのものを作っています。本。本は箱に入れるときの立体的な並べ方が違うので、どのように並べて入れるかで、何冊入るか決めたいです。LimitDeterminer は、それらがすべて同じ方向に配置されている場合に適合する数を決定し、既にそこにあるものと比較します。ブリーフィングだけで、コードは完全ではありません。私はイライラして混乱しているため、間違いを修正するためにここに来ました。また、関数にオブジェクトを常に指定する必要があるため、何か間違っていると思います...

ありがとう

<?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);
?>
4

2 に答える 2

7

オブジェクト指向コードは、手続き型コードとはまったく異なる考え方です。すでにおなじみだと思います。本質的に、「真の」OO の背後にある考え方 (これは主に私の解釈です) は、作成した各オブジェクトが物事を「実行」し、物事について「知っている」ということです。多くの場合、その種類のオブジェクトが、要求したことを実際に認識または実行するかどうかによって、物事を論理的で理解しやすい方法で行っているかどうかを判断できます。

たとえば、ボックスにはstoreThingsを格納でき、ボックスのsetDimensionsを設定できます。box内でgetThingsを取得することもできますが、storedPermsはあまり意味がなく、 CalcArrange も意味がありませ。それらについては少し後で説明します。


__construct 関数を使用すると、作業が少し楽になります。Book または Box を作成するときはいつでも、二次関数を呼び出さなくても、初期の寸法/構成を設定できます。

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 );
    }

}

そして本に…

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;
    }

}

これにより、次のようにブックとボックスを作成できます。

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

ここから、おそらく次のようなものを使用できます。

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;
    }
}

そして、次のように言って実行できます。

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

本をさまざまな方法で回転させて、絶対最大容量を決定したい場合は、そのまま実行してください。

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;

        }

    }
}

次に、getCapacity 関数のバリエーションを記述して、それを利用できます...

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 ) );
        }

    }

そして出来上がり!新しい関数 (I にドットを付けて T を正しく交差させたと仮定します) は、次のように実行できます。

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

最後に、ここではセマンティクスが非常に重要です。私はコーディングに時間の約 10% を費やして、メソッドまたは変数を呼び出すのに最適なものを考えています。また、可能な限りコードの重複を避けてください。上記の例では、getCapacity 関数を実装しました。これは後で getMaxCapacity によって使用され、時間を節約し、getMaxCapacity 関数を簡素化しました。

それが役立つことを願っています!

于 2012-06-03T03:20:31.367 に答える
1

このチュートリアルは初心者に適しています...

http://net.tutsplus.com/tutorials/php/object-Oriented-php-for-beginners/

于 2012-06-03T03:27:01.287 に答える