doushih06137
2016-08-03 07:51
浏览 30
已采纳

在PHP中扩展类 - 奇怪的行为

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.

图片转代码服务由CSDN问答提供 功能建议

首先,引用PHP手册( http://php.net/manual/en/keyword.extends.php ):

  扩展或派生类具有
base类的所有变量和函数(这被称为'继承',尽管事实上没有人),以及您在扩展定义中添加的内容。
   
  
 

那么为什么这个简单的例子不起作用:

 &lt;?php 
 
class A 
 {
 private function a()  
 {
 echo'a';  
 
 
 
公共函数b()
 {
 echo'b';  
 
 
 
} 
 
 
class B扩展A 
 {
 //没有扩展定义,只有继承的
} 
 
 $ object_B = new B(); 
 
echo  $ object_B-&GT; b();  //致命错误:从无效上下文调用私有A :: a()
?&gt; 
   
 
 

经过一些实验,结果发现删除 使其有效。 我甚至没有在任何地方调用它。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • duanchen7036 2016-08-03 07:58
    已采纳

    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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dqsxsmi3704 2016-08-03 07:59

    "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.

    评论
    解决 无用
    打赏 举报
  • duanbai5348 2016-08-03 07:59

    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(); 
    
    评论
    解决 无用
    打赏 举报
  • doucan8276 2016-08-03 08:02

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题