-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 回答