函数在面向对象的PHP结构中没有返回值


function not returning value in object oriented php structure

我是一个非常新的面向对象的PHP,我正在通过一些基本的例子学习。我有index.php文件中,我已经创建了类和使用的getter和setter。在class_lib.php文件中,我已经创建了类的对象,并尝试回滚该值。但是我没有得到任何值返回。

inde.php

<?php
class person {
var $name;
 function __construct($persons_name) 
 {
        $this->name = $persons_name;
 }
 function set_name($new_name) 
 {
        $this->name = $new_name;
 }
 function get_name() 
 {
        return $this->name;
 } 
}
class employee extends person
{
    function __construct($employee_name)
    {
        $this->set_name($employee_name);
    }
}
?>

class_lib.php

<?php
include "index.php";
$stefan = new person("Stefan Mischook");
echo "Stefan's full name: " . $stefan->get_name();
echo '<br>';
$james = new employee("Johnny Fingers");
echo "new employee---> " . $james->get_name();
?>

将class_lib.php代码更改为index.php代码,将index.php代码更改为class_lib.php

index . php

<?php
    include "class_lib.php";
    $stefan = new person("Stefan Mischook");
    echo "Stefan's full name: " . $stefan->get_name();
    echo '<br>';
    $james = new employee("Johnny Fingers");
    echo "new employee---> " . $james->get_name();
?>

class_lib.php

<?php
    class person {
    var $name;
    function __construct($persons_name) 
    {
        $this->name = $persons_name;
    }
    function set_name($new_name) 
    {
        $this->name = $new_name;
    }
    function get_name()
    {
        return $this->name;
    }
    }
    class employee extends person
    {
    function __construct($employee_name)
    {
        $this->set_name($employee_name);
    }
    }
 ?>

下面是在PHP 5.4.4上执行的代码:http://ideone.com/KvrQVF

输出:

Stefan's full name: Stefan Mischook
new employee---> Johnny Fingers

似乎工作正常