2

First, quote from PHP manual (http://php.net/manual/en/keyword.extends.php):

The extended or derived class has all variables and functions of the
base class (this is called 'inheritance' despite the fact that nobody
died) and what you add in the extended definition.

So why is that this simple example doesn't work:

<?php

class A
{
    private function a() 
    { 
        echo 'a'; 
    }

    public function b() 
    { 
        echo 'b'; 
    }

}


class B extends A 
{
    //no extended definition, only what's inherited 
}

$object_B = new B();

echo $object_B->b(); // fatal error: Call to private A::a() from invalid context 
?>

After some experimenting, it turns out that removing method a from class A makes it work. And I'm not even calling it anywhere.

confused
  • 23
  • 4

4 Answers4

3

You can use a method of the same name as a constructor with a PHP class. So, your method a is acting as a constructor for class A.

Rename your method, and it should work:

class First
{
    private function another() 
    { 
        echo 'a'; 
    }

    public function b() 
    { 
        echo 'b'; 
    }
}

See __construct() vs SameAsClassName() for constructor in PHP

Community
  • 1
  • 1
Zac Brown
  • 459
  • 4
  • 12
  • Actually, I didn't want to make a constructor :) And starting from PHP7, __construct is the only right way to write them. I wanted to write a simple method to test inheritance. And it didn't occur to me *at all* that I was using the same name for method as for class name (method was named **a**, and class was named **A**) I think mistakes like this keep happening simply because case-sensitivity is not consistent in PHP. – confused Aug 03 '16 at 08:24
  • Yea, I figured you weren't trying to make a `__constructor`, which is why I suggested you change the name of your `a` method. This is one of those less known "quirks" about PHP. In PHP7, you're right. However, most environments aren't using 7 yet, so this is still something to keep in mind. Did my answer address your question, though? :) – Zac Brown Aug 03 '16 at 08:26
  • Yes, I accepted your answer. But to clarify even more, because I've lost lots of time trying to figure this out (and I'll probably forget it very soon): 1. Function names, method names and class names are NOT case-sensitive in PHP 2. Variable names, constant names, class properties and class constants ARE case-sensitive in PHP So many quirks in the most popular programming language for the most important medium on the planet. – confused Aug 03 '16 at 09:03
0

"Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP" - Reports PHP 7 after executing the sample code.

I think you can't name the method with the same name as the class. In older PHP-Versions you could define the constructor of the class by naming the method like the class. PHP assumes the functions a to be a constructor. So it won't work that way.

Like Zac Brown said, you have to use the __construct() method.

Davide Perozzi
  • 386
  • 2
  • 13
0

In class A you have define a() method it with same name of class. so it is constructor method of A class. but in PHP you can not make private constructor method. your code should be like this.

<?php
class A
{
    public function a() 
    { 
        echo 'a'; 
    }

    public function b() 
    { 
        echo 'b'; 
    }

}


class B extends A 
{
    //no extended definition, only what's inherited 
}

$object_B = new B();

echo $object_B->b(); 
Haresh Vidja
  • 8,340
  • 3
  • 25
  • 42
0

Here's the clarification:

You are defining method a() in Class A. They have the same name so method a() is treated as constructor of Class A. Therefore, the moment you initialize Class B in $object_B = new B();, you initialize Class A too since A is extending B and the constructor method is called. Because Classes which have a constructor method call this method on each newly-created object PHP Constructor. and hence the error.

I believe this clarifies your doubt.

jonju
  • 2,711
  • 1
  • 13
  • 19