php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52783 error on construct's Access level
Submitted: 2010-09-06 16:45 UTC Modified: 2010-09-06 18:01 UTC
From: augustowebd at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.3 OS: Linux nvirus 2.6.28-19-generic
Private report: No CVE-ID: None
 [2010-09-06 16:45 UTC] augustowebd at gmail dot com
Description:
------------
given an abstract class Foo which has a constructor public:

abstract class Foo{
 public function __construct (){
    echo "Foor ::";
 }
}

and a concrete class(Bar) that extends Foo, but the latter has its constructor protected:

final class Bar extends {
  protected function __construct () {
   parent:: __construct (); 
   echo "bar";
  }
}

It generates a fatal error.

This is expected behavior of normal methods, but not for constructors

Test script:
---------------
abstract class Foo{
    public function __construct(){
        echo "Foo::";
    }
}

final class Bar extends Foo{
    protecte function __construct(){
        parent::__construct();
        echo "bar";
    }
    
    public static function bar(){
       new self();
    }
}

Bar::bar();

Expected result:
----------------
Foo::bar

Actual result:
--------------
Fatal error: Access level to Bar::__construct() must be public (as in class Foo) in /var/www/index.php on line 17 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-09-06 18:01 UTC] cataphract@php.net
-Status: Open +Status: Bogus
 [2010-09-06 18:01 UTC] cataphract@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

In PHP, constructors have the same checks as other methods on inheritance, with the exception mentioned in the manual (no arguments check, except if the constructor is specified in an interface).
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 17:01:34 2025 UTC