简体   繁体   English

子类功能的父类继承

[英]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. 我正在为wordpress写一个电源插件,该插件基本上提供了许多使开发更容易的功能。

Don't worry about the wp stuff though, this is a PHP question. 尽管不用担心wp的问题,这是一个PHP问题。 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. 我有一个主类'my_wp_funcs',还有一些其他的大类可以做不同的事情,这些我分别编写并可以单独工作,例如:插入新帖子。

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. createpost类扩展了funcs类,其他类也扩展了func。

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. 您不能在抽象类中将方法定义为abstract ,然后在同一类中将其真正定义。 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) (它们都必须是公开的,并且必须接受相同的论点)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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