php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31943 error calling an abstract method from another method in the same class
Submitted: 2005-02-12 00:46 UTC Modified: 2005-02-12 17:47 UTC
From: alberto dot delatorre at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.0.3 OS: Windows XP SP2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: alberto dot delatorre at gmail dot com
New email:
PHP Version: OS:

 

 [2005-02-12 00:46 UTC] alberto dot delatorre at gmail dot com
Description:
------------
If you have an abstract method inside an abstract class (obvious) and you call that method inside another non-static method within the class, you get an error.

If this is an abstract class, it is suposed that a non-static method defined in the class, may have been called from an instance of a child class of that class (cause this class is abstract), and this child class may not be abstract, to be instantiated. So the abstract method have must been overloaded in the non-abstract child class and the call in the non-static method must be valid.

I encountered a waste solution, making a reference to $this in the marked lines, so the error is not be triggered.

If we substitute the marked line with:
       $aux_this=&$this;
       $aux_this->_foo();
The error doesn't appear.

Sorry for my english, if it is embarrasing.

Reproduce code:
---------------
<?php
abstract class A {
   public function foo() {
       $this->_foo(); // THIS LINE IS MARKED ;)
   }
   
   private abstract function _foo();
}

class B extends A {
   private function _foo(){
   		echo "foo!!";
   }
}

$b=new B();
$b->foo();
?> 

Expected result:
----------------
foo!!

Actual result:
--------------
Fatal error: Cannot call abstract method A::_foo() in myphp.php on line 4

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-12 00:49 UTC] alberto dot delatorre at gmail dot com
Sorry, the visibility in both _foo definitions must be protected, not private.
 [2005-02-12 16:39 UTC] alberto dot delatorre at gmail dot com
So i tried again and now it's working. Sorry for my stupid bug sent.

Well, anyway, if the error is caused of a visibility problem, i think the error message is wrong, because it says it is an abstract method call problem.
 [2005-02-12 17:47 UTC] tony2001@php.net
No bug here.
Fill a new report if you think there is a bug somewhere else.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 23 04:01:32 2024 UTC