-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?

6
  • 3
    php.net/manual/en/language.types.object.php Commented Feb 22, 2013 at 23:00
  • 2
    You are looking for new stdClass() instead of Object(). See also php.net/manual/en/language.oop5.basic.php Commented Feb 22, 2013 at 23:00
  • I tried that method first but i got Call to undefined function stdClass() Commented Feb 22, 2013 at 23:01
  • I also got Cannot use object of type stdClass as array Commented Feb 22, 2013 at 23:03
  • @Dave If you got undefined function it means you forgot the new keyword. $block[$i] = new stdClass() Commented Feb 22, 2013 at 23:04

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

9 Comments

Hmm not seeing how that would allow me to create a list of objects from a loop =/
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.
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
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.
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.
|
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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.