简体   繁体   中英

Parent class inheritance of child class functions

I'm writing a power plugin for wordpress that basically supplies a bunch of functions to make development easier.

Don't worry about the wp stuff though, this is a PHP question. I have one master class 'my_wp_funcs', and a few other large classes that do different things, which I've written separately and work on their own, for example: insert a new post.

I would like to be able to use this syntax:

$wpfuncs = new funcs;
$wpfuncs->createpost($args);
$wpfuncs->addimage();

where createpost class extends funcs class, along with other classes that extend funcs too.

I've been reading up on abstraction, but am getting continual errors. Here's a trimmer version of what I have:

<?php

$wpfuncs = new funcs;
$wpfuncs->createpost($args);

abstract class funcs
{
    abstract protected function createpost();

    public function createpost($args){
        $tool = new $this->boss_posttype('derp', 'derps');
    }
}

class createpost extends funcs{
    public function __construct(){
        //do stuff
    }
}

Cheers for any help!

You can't define the method as abstract in the abstract class and then define it for real in the same class. You also can't call the abstract class directly.

You probably want something like this:

abstract class funcs
{
    abstract public function createpost($args);
}

class myFuncs {
    public function createpost($args){
        $tool = new $this->boss_posttype('derp', 'derps');
        // do other stuff
    }
}


$wpfuncs = new myFuncs();
$wpfuncs->createpost($args);

Note that your implementation goes in your own class, and that implementation has to match your abstract definition. (they both have to be public and they have to accept the same arguments)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM