PHP - 类对象错误

时间:2012-03-04 02:11:34

标签: php class object

我创建了一个php人类和一个非常基本的表单。表单是将数据输出到另一个页面。这是一个缩小版本,它没有包含所有编码,但我想我有它的主旨。我无法弄清楚它为什么不起作用。它不能是什么大事我也不认为我错过了任何步骤。请注意下面的代码:

我根本没有收到任何错误......键入表单的数据不会呈现给我的proceslogin.php页面。

这是我的表格。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
</head>
                <div>
                    <form id="login" method="post" action="proceslogin.php">
                    <label for="firstname"> First Name: </label>
                    <input type="text" id="firstname" name="firstname" maxlength="100" tabindex="1" />

                    <label for="lastname"> Last Name: </label>
                    <input type="text" id="lastname" name="lastname" maxlength="100" tabindex="2" />

                    <input type="submit" id="submit" name="submit" value="Retrieve Full Name"/>

                    </form>

                </div>

<body>
</body>
</html>

这是我的班级

<?php
//Person Class
class Person{
    //attributes for the first and last name of the class
    private $firstname;
    private $lastname;

    /*Constructs the function*/
    public function __construct(){
    }

    /*Destroys the function*/
    public function __destruct(){
    }

    /*Get function*/
    public function __get($name) {
        return $this->$name;
    }

    /*Use the set function*/
    public function __set($name, $value) {
        $this->$name=$value;
    }

    /*Whis is what retrieves the values from memmory*/
    public function retrieve_full_name() {
        $fullname = $this->firstname . ' ' . $this->lastname;
        return $fullname;
    }
} //End of class
?>

这是类对象要呈现的页面。

    <?php

        require_once('websiteconfig.inc.php');

    ?>





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Process Login</title>
</head>
<div class="container" id="shadow">
<div>
        <?php 
        //include for the header
            require_once(ABSOLUTE_PATH . 'header.inc.php');

            //This is the inlcude for the class files
            require_once('class/person.class.php');

            //Instantiate person class
            $person = new Person();

            //sets atributes for both firs and last names
            $person->firstname = $_POST['$firstname'];
            $person->firstname = $_POST['$lastname'];
        ?>
</div>

<p> 

<?
echo 'Your full name is ' . $person->retrieve_full_name() . ' .' ;
?> 


</p>
    <div>

    </hr>

    </div><!--End of Body-->
            <?php 
            //include for the footer
                require_once(ABSOLUTE_PATH . 'footer.inc.php');
            ?>
</div><!--end of header-->

<body>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

$lastname变量中提取时,您正在使用$firstname_POST,该变量应该是名称。你也有名字设置姓氏应该在哪里。

所以这就是你的php代码的样子。

PS:看看Smarty,它将有助于清理这一切。

<?php
        require_once('websiteconfig.inc.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Process Login</title>
</head>
<div class="container" id="shadow">
  <div>
    <?php 
        //include for the header
            require_once(ABSOLUTE_PATH . 'header.inc.php');

            //This is the inlcude for the class files
            require_once('class/person.class.php');

            //Instantiate person class
            $person = new Person();

            //sets atributes for both firs and last names
            $person->firstname = $_POST['firstname'];
            $person->lastname = $_POST['lastname'];
        ?>
  </div>
  <p>
    <?
echo 'Your full name is ' . $person->retrieve_full_name() . ' .' ;
?>
  </p>
  <div>
    </hr>
  </div>
  <!--End of Body-->
  <?php 
            //include for the footer
                require_once(ABSOLUTE_PATH . 'footer.inc.php');
            ?>
</div>
<!--end of header-->

<body>
</body>
</html>

答案 1 :(得分:0)

不确定你的意思,但是你要设置两次名字:

$person->firstname = $_POST['$firstname'];
$person->firstname = $_POST['$lastname'];

要检查的另一件事是$ _POST ['$ firstname']是否有值,请尝试回显它