-3

I'm having difficulty understanding how to create objects in my script.... i get this error :

PHP Fatal error:  Call to undefined function Object()

My code is like this:

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


}

Can some one explain what i'm doing incorrectly?

Sir
  • 8,135
  • 17
  • 83
  • 146

2 Answers2

2

In the simplest form, objects are classes.

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    

Start reading about them here: http://www.php.net/manual/en/language.oop5.basic.php

sjdaws
  • 3,466
  • 16
  • 20
  • Hmm not seeing how that would allow me to create a list of objects from a loop =/ – Sir Feb 22 '13 at 23:02
  • I think you need to ask yourself 'why am I trying to create objects from a loop?'. If you don't understand an object, I don't see why you're trying to create them or what purpose they will serve. – sjdaws Feb 22 '13 at 23:06
  • Well i want to loop through them because they hold X and Y co ordinates... it will save loop time if i use loop objects rather than an array. Because i can just check if object[$x] exists - if not then no need to keep checking.. if it was an array i have to check them all to be 100% sure the value don't exist – Sir Feb 22 '13 at 23:06
  • What you're explaining is an array. You want to check if array[$x] exists. I still don't understand why you're trying to create heaps of objects that offer nothing further than what you get from an array. If you're checking keys, it will only exist once in an array since you can't duplicate keys. – sjdaws Feb 22 '13 at 23:11
  • Because creating arrays like this: `$myarray[$x][$y];` to store co ordinates will general alot of `Undefined offset` warnings so my alternative option was to create objects. – Sir Feb 22 '13 at 23:13
  • I have updated my answer to store XY's in the `object`, however if you notice... in the end it's still an `array`. – sjdaws Feb 22 '13 at 23:33
  • So now object[x][y] will hold an array? even if empty? EDIT oh i kinda get it although i have to edit to input X then Y rather than in the same call :) – Sir Feb 22 '13 at 23:36
  • Correct, by default it's just an empty array so `$myarray` is empty to start, until you add an `X` and `Y`. It's just a starter, but should get you moving in the right direction, read that oop link at the bottom of my answer. So init the object once `new coOrds();` and in your loop, `saveXY` and optionally check the result if you want to check whether it already exists or not. – sjdaws Feb 22 '13 at 23:40
  • Yeah im beginning to understand a bit better. Not the most elegant method compared to most languages guess its still new to PHP – Sir Feb 22 '13 at 23:41
0

You need to define classes and instance them as objects:

    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);
Lighthart
  • 3,648
  • 6
  • 28
  • 47