-3

スクリプトでオブジェクトを作成する方法を理解するのに苦労しています...このエラーが発生します:

PHP Fatal error:  Call to undefined function Object()

私のコードは次のようなものです:

$block = Object();  // error here
$row['x'] = 5;
$row['y'] = 7;
$row['widthx'] = 3;
$row['widthy'] = 3;

for($i = $row['x']; $i < ($row['x'] +  $row['widthx']); $i++){

    if(!is_object($block[$i])){
        $block[$i] = Object();
    }


}

誰かが私が間違っていることを説明できますか?

4

2 に答える 2

2

最も単純な形式では、オブジェクトはクラスです。

class coOrds {

    // create a store for coordinates
    private $xy;

    function __contruct() {

        // it's still an array in the end
        $this->xy = array();

    }

    function checkXY($x, $y) {

        // check if xy exists
        return isset($this->xy[$x][$y]);

    }

    function saveXY($x, $y) {

        // check if XY exists
        if ($this->checkXY) {

            // it already exists
            return false;

        } else {

            // save it
            if (!isset($this->xy[$x])) {

                // create x if it doesn't already exist
                $this->xy[$x] = array();

            }

            // create y
            $this->xy[$x][$y] = '';

            // return
            return true;

        }

    }

}

$coords = new coOrds();

$coords->saveXY(4, 5); // true
$coords->saveXY(5, 5); // true
$coords->saveXY(4, 5); // false, already exists    

ここでそれらについて読み始めてください:http ://www.php.net/manual/en/language.oop5.basic.php

于 2013-02-22T23:01:42.290 に答える
0

クラスを定義し、それらをオブジェクトとしてインスタンス化する必要があります。

    class Object {
        private $name;

        __construct($name){
            $this->name=$name
        }

        public function setName($name)
        {
            $this->name = $name;

            return $this;
        }

        public function getName()
        {
            return $this->name;
        }
    }

    $block = $new Object($name);
于 2013-02-22T23:03:52.823 に答える