在php中调用一个从子类到父类的函数[关闭]

please check these classess:

class DB
{   
    protected saveINDB($name)
    {
       //insert $name to DB
    }

}

class Items extends DB
{
    private $name;

    function __construct($name) 
    {
          $this->name = $name;        
    }        
}

I want save $name in 'Item' to database via a method in 'DB' class.

I think I can do it like this:

$item = new Item('Free');
$item->saveINDB($item->name);

is it right?

No how can I call function automaticly in second class?

for example...when I create 'Item' object. it call saveINDB() in 'DB' class in save $name in database.(like propertise in __construct() method)

something like this:

class Items extends DB
{
    private $name;

    function __construct($name) 
    {
          $this->saveINDB($name);        
    }        
}

I hope you underestand me.

$this->saveINDB($name); will work fine in the constructor, as per your example. There's no need to use parent:: unless you're overriding the method, because the whole point of extending a class is that the child inherits all the methods of the parent.

There is an error in your DB class though, which might be causing you issues:

protected function saveINDB($name) // function was missing

Here's a working demo on codepad

Do you mean parent::saveINDB($name); ?