PHP:致命错误:无法实例化抽象类 [英] PHP: Fatal error: Cannot instantiate abstract class

查看:45
本文介绍了PHP:致命错误:无法实例化抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为:

的抽象数据库类

抽象类数据库{受保护的 $value;}

我创建了另一个抽象类

抽象类 my_database 扩展数据库 {公共函数 set_value($value) {$this->value = $value;}}

当我尝试使用它时:

$my_db = new my_database();

出现错误:

致命错误:无法在...中实例化抽象类 my_database

我尝试做的是:抽象类数据库有一个受保护的 $value,我想创建一个包装类,以便能够(临时)更改受保护的值.>

我该怎么做?

不幸的是,早些时候,当我尝试不使用抽象的 my_database 时,出现错误:

- 抽象方法,因此必须声明为抽象或已实现- 抽象函数不能包含主体

从my_database中完全取出抽象词后,出现如下错误:

<块引用>

致命错误:类 my_database 包含 32 个抽象方法并且必须因此被声明为抽象或实现剩余的方法

我该如何解决这个问题?

解决方案

定义为抽象的类不能被实例化,任何包含至少一个抽象方法的类也必须是抽象的.您可以在此处的 PHP 文档中阅读相关内容:link

这是一个例子.

有一个抽象类(注意抽象方法没有主体——它们不能有主体——它只是一个签名):

抽象类 AbstractClass{//强制扩展类定义此方法抽象保护函数 getValue();抽象保护函数 prefixValue($prefix);//常用方法.它适用于所有儿童——他们不必再次申报.公共函数打印输出(){打印 $this->getValue() ."
";}}

用这样的类扩展你的抽象类(注意所有抽象方法必须在具体类中定义):

class ConcreteClass1 扩展 AbstractClass{受保护的函数 getValue() {返回 "ConcreteClass1";}公共函数 prefixValue($prefix) {返回{$prefix}ConcreteClass1";}}

然后你可以创建 ConcreteClass1 的实例:

$class1 = new ConcreteClass1;

I have an abstract database class named as:

abstract class database {
  protected $value;
}

I created another abstract class

abstract class my_database extends database {
  public function set_value($value) {
    $this->value = $value;
  }
}

When I try to use it:

$my_db = new my_database();

I get error:

Fatal error: Cannot instantiate abstract class my_database in ...

What I try to do is: The abstract class database has a protected $value and I would like to create a wrapper class, to be able to change the protected value (temporarily).

How can I do that?

EDIT1: unfortunately earlier, when I tried without abstract my_database, I got the errors:

- abstract methods and must therefore be declared abstract or implemented
- Abstract function cannot contain body

EDIT2: After taking out the abstract word completely from my_database, I got the following error:

Fatal error: Class my_database contains 32 abstract methods and must therefore be declared abstract or implement the remaining methods

How can I fix this?

解决方案

Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract. You can read about this in PHP's documentation here: link

Here's an example.

There is an abstract class (note that abstract methods don't have body - they CAN'T have body - it's just a signature):

abstract class AbstractClass
{
    // Force Extending class to define this method
    abstract protected function getValue();
    abstract protected function prefixValue($prefix);

    // Common method. It will be available for all children - they don't have to declare it again.
    public function printOut() {
        print $this->getValue() . "
";
    }
}

Extend your abstract class with a class like this (note that all abstract methods MUST be defined in concrete class):

class ConcreteClass1 extends AbstractClass
{
    protected function getValue() {
        return "ConcreteClass1";
    }

    public function prefixValue($prefix) {
        return "{$prefix}ConcreteClass1";
    }
}

Then you can create instance of ConcreteClass1:

$class1 = new ConcreteClass1;

这篇关于PHP:致命错误:无法实例化抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆