简体   繁体   中英

Calling an Abstract method from parent abstract class

After looking at this question , I tried the chosen answer myself! So basically what I wrote was `

abstract class person
{

  function __construct()
  {
    // some code here
  }

  function myfunc()
  {
    $this->test();
  }

  abstract function test();
}

class employee extends person
{
  function __construct()
  {
    parent::__construct();
  }

  function test()
  {
    echo "So you managed to call me !!";
  }

}


$employee = new employee();
$employee->myfunc();`

When I run this script, I got an usual error Fatal error: Using $this when not in object context on line 7.

So, how do I call an Abstract from the Parent Class?

  • EDIT

The real thing which I am trying to do is create a DatabaseObject class which holds all the common methods of different database objects and make classes extend it.

abstract class DatabaseObject {
   abstract protected function getTableName();

   public static function find_by_id($id = 0){
        global $database;
        $class = get_called_class();

        $result_array = self::find_by_sql("SELECT * FROM ". $this->getTableName() ." WHERE _id = {$id} LIMIT 1");
        echo $result_array . "<br />";
        return !empty($result_array) ? array_shift($result_array) : false;
    }

    public static function find_by_sql($sql=""){
        global $database;
        $result_set = $database->query($sql);
        $object_array = array();
        while ($row = $database->fetch_array($result_set)){
            $object_array[] = self::instantiate($row);
        }
        return $object_array;
    }
   }
}

So this is some part of my DatabaseObject Class. My User class is as follows

class User extends DatabaseObject {
    protected static $table_name = "users";

    protected function getTableName(){
        global $table_name;
        return $table_name;
    }
}

Now, whenever I do

$user = new User();
$user->findById(1);

I get the error Fatal error: Using $this when not in object context on the line which points to

$this->getTableName();

I hope this might help in clearing up some stuff. Is there any way I can achieve this? Thanks!

Your code example works just fine, I don't understand your issue Demo .

So the code you've shared is not the code you talk about with the error .

You can not call the variable as if it were a member of the class, since you declared it static. Calls to static variables looks the same as when you call static functions, use the :: operator.

class User extends DatabaseObject {
    protected static $table_name = "users";

    protected function getTableName(){
        return self::$table_name;
    }
}

You cant access the abstract function inside the same abstract class.

What ever your needs are, you have to call that method in sub class only, abstract super class will not call the sub class function.

EDIT:

Because the function dosent exists in that class.

The first thing is you can not call the abstract function within the same class. You are calling test(); in side myfunc() which is not allowed by oops.

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