继承在PHP中不起作用 [英] Inheritance not working in PHP
本文介绍了继承在PHP中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友你好。我是PHP的新手。我在PHP中尝试继承,但我无法理解一些概念。当我从子类调用print_name方法时,没有任何内容被打印出来。任何人都可以告诉我哪里出错了吗?
Hello friends. I am new to PHP. I'm trying inheritance in PHP but I'm not able to understand some concepts. When I am calling print_name method from child class nothing gets printed. Can anyone tell me where I am going wrong?
class Parent_class
{
var $first_name="David";
var $last_name="Hussey";
function __construct()
{
echo "Parent class object instantiated" . "</br>";
}
function print_name($name,$surname)
{
$this->first_name=$name;
$this->last_name=$surname;
echo $this->first_name . " ". $this->last_name . "</br>";
}
}
class Child_class extends Parent_class
{
var $x="Roger";
var $y="Nadal";
function __construct()
{
echo "Child class object instantiated" . "</br>";
}
function call_parent_class_function()
{
echo $this->x . " ". $this->last_name . "</br>";
$this->print_name($x,$y);
}
}
$obj = new Child_class;
$obj->call_parent_class_function();
$obj1 = new Parent_class;
$obj1->print_name("Virat","Kohli");
推荐答案
first_name = David;
var
first_name="David"; var
last_name = Hussey;
函数__construct()
{
echo 实例化父类对象。 < / br>;
}
函数print_name(
last_name="Hussey"; function __construct() { echo "Parent class object instantiated" . "</br>"; } function print_name(
name,
这篇关于继承在PHP中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文