search

Home  >  Q&A  >  body text

class - How does PHP implement that all subclass attributes have toArray() method?

How to implement in PHP that all properties of subclasses have toArray() method?

After calling, it becomes an array

How to design the parent class? ?

阿神阿神2864 days ago620

reply all(3)I'll reply

  • 高洛峰

    高洛峰2017-05-16 13:01:22

    interface Arrayable {
        public function toArray();
    }
    
    class parent implents Arrayable {
        public function toArray() {
            // to do
        };
    }
    
    class children extends parent{
    
    }

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-16 13:01:22

    Just write a toarray method for the parent class, that’s it,

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 13:01:22

    Write an interface implementation, or use the parent class for inheritance

    reply
    0
  • Cancelreply