my looking at a new project that'll have lots of objects, people, classes, students, employees.

With this, I'll need to also pull 'people' into lists. remove them, add them etc.

Is it possible to have a class of objects which is generic?

Sometihng like this?

<?php

class listOf_Object{
    public $obj;
    public $mylist;

    public function removeObj($obj){
        unset($mylist[$obj]);
    }

    public function addObj($obj){
        array_push($mylist, $obj);
    }

    public function showList(){
        foreach ($mylist as $o){
            echo $o . PHP_EOL;
        }        
    }

}


$employeeList = new listOf_Object();
$employeeList->addObj($employee);

    Solved but I welcome any feedback

    <?php
    
    class listOf_Object{
        public $obj;
        public $mylist = [];
    
     
        public function removeObj($obj){
            unset($mylist[$obj]);
        }
    
        public function addObj($obj){
            array_push($this->mylist, $obj);
        }
    
        public function showList(){
            foreach ($this->mylist as $o){
                echo $o->name . PHP_EOL;
            }  
            var_dump($this->mylist);      
        }
    
    }

      My initial inclination is that it's overkill, since you can just use an array variable and be good to go. However, I might be tempted to do it if there is other functionality you want to add, maybe even then extending it into type-specific child classes if appropriate.

      It might be handy to at least restrict it to only allow a certain type of object. E.g.: you could maybe do this:

      class ListOfThings
      {
        private $list = [];
        private $className;
      
        public function __construct($className)
        {
          $this->className = $className;
        }
      
        public function addThing($object)
        {
          if (is_a($object, $this->className)) {
            $this->list[] = $object;
          } else {
            throw new Exception("Invalid object type");
          }
        }
      
        public function removeThing($key)
        {
          if(isset($this->list[$key])) {
            unset($this->list[$key]);
          }
          else {
            throw new Exception("Invalid list key");
          }
        }
      
        public function getAll()
        {
          return $this->list;
        }
      }
      

        Yeah that makes sense, how do you remove an object though? What is the key of any given object when the object is not known?

        But you do know the object, it's been given. If it's the key that's not known, it's just a matter of searching the array for it.

        Of course, depending on how careful or not you are, the object might be in there more than once, in which case there might be several keys that need unsetting.

          nzkiwi80 What is the key of any given object when the object is not known?

          If there's some unique attribute of each such object being added, maybe you could add that as a $key argument to the addThing() method, perhaps? At that point, though, I'd be tempted to make it a type-specific instance of that class, and then the addThing() method would be customized to extract that property from the inserted object and use it as the array key.

            Write a Reply...