-1

Alright so I got 2 problems on my hands at the moment and those are

  • I want to call the function from the parent object but I am getting a lot of errors saying "Fatal error: Cannot instantiate abstract class Person"
  • If I call the getUserItems directly it will not do anything. There wont be anything echoed or such.


<?php

    abstract class Person {
        abstract function getUserItems();
    }

    class inventory extends Person {

        protected $storage = array();
        protected $item_id;

        public function itemAdd($itemname) {
            $storage[$this->item_id+1] = $itemname;
        }

        public function getUserItems() {
            foreach($this->storage as $itemName=>$item_id) {
                echo $itemName." ".$item_id."<br/>";
            }
        }
    }

    $user = new Person();

    $user->getUserItems();

    /*$user = new inventory();
    $user->itemAdd("Item 1");
    $user->itemAdd("Item 2");

    $user->getUserItems();*/

?>
Dekel
  • 60,707
  • 10
  • 101
  • 129
Tommy
  • 3
  • 3
  • 2
    You're trying to create an instance of Person (`new Person()`). But the Class "person" is declared as "abstract". Remove the "abstract" and it should work,. – UeliDeSchwert Oct 31 '16 at 11:37
  • 2
    `Fatal error: Cannot instantiate abstract class Person` is very clear. ` $user = new Person();` is wrong. http://php.net/manual/en/language.oop5.abstract.php – bassxzero Oct 31 '16 at 11:38

3 Answers3

1

In OOP - abstract class may not be instantiated.

An abstract class is a class with both implementation and interface (pure virtual methods) that will be inherited. Interfaces generally do not have any implementation but only pure virtual functions.

So you cannot instantiate (new Person()). You must extend this abstract class and implement it's abtract functions (same way you did in inventory).

Regarding the echoing problem - inside your itemAdd function you didn't use the object's $storage member (you just used a local $storage variable). You should use it as the object's memebr, using $this->storage:

public function itemAdd($itemname) {
    $this->storage[$this->item_id+1] = $itemname;
}

Note that I'm not so sure how you managed the $this->item_id member because you didn't set/changed it in the code.

If you only want to add new items to the storage member you could use:

$this->storage[] = $itemname;

This will make sure every new item will be added to the $storage array.

Community
  • 1
  • 1
Dekel
  • 60,707
  • 10
  • 101
  • 129
  • Yes but how do I assign every one of them to an id? Thats why I used $this->item_id – Tommy Oct 31 '16 at 11:52
  • If each of them have a specific `id` you can use `itemAdd($item_id, $item_name) ` and inside the function to use the `$item_id`. – Dekel Oct 31 '16 at 11:53
  • Hmm yeah I get that, but for example if I wanted to use $person->getUserItems() how do I achieve it? For example if I use person extends inventory I will get an error saying that it cannot find that class etc. Example code: http://pastebin.com/ni3QMdtD – Tommy Oct 31 '16 at 12:01
  • You ask a different question here (and I'm not so sure I understand what you ask). Meanwhile - if the answer is correct (based on your original question) you should accept it. And try to clarify your question so I might be able to help. – Dekel Oct 31 '16 at 12:04
0

ad1)

Person is abstract class so you can not create object of that class.

ad2)

You must use $this

so not:

 $storage[$this->item_id+1]

but

$this->storage[++$this->item_id]

Here I fixed another bug which was $this->item_id+1

nospor
  • 4,190
  • 1
  • 16
  • 25
0

Abstract class can not access directly but access from other class.

abstract class Person {
    abstract function getUserItems();
}

class inventory extends Person {

    protected $storage = array();
    protected $item_id=2;

    public function itemAdd($itemname) {
        $storage[$this->item_id+1] = $itemname;
    }

    public function getUserItems() {
        foreach($this->storage as $itemName=>$item_id) {
            echo $itemName." ".$item_id."<br/>";
        }
    }
}

$user = new inventory();

$user->getUserItems();

$user = new inventory();
$user->itemAdd("Item 1");
$user->itemAdd("Item 2");

$user->getUserItems();
madankundu
  • 154
  • 8