我是PHP新手,一直在試圖獲得一些經驗。我有C++經驗,但已經有一段時間了,所以這是一個複習。在PHP中使用對象而不先實例化
因此,我偶然發現了JPMaster的PHP登錄腳本,並且在註冊表單中我感到困惑。如果您查看HTML表格,則會使用$ form對象,但它不會在此頁面上的任何位置實例化,但從我所知道的對象也可以從這裏開始,因爲它是添加新信息而不是僅僅通過。在查看'session.php'(登錄系統中的另一個文件)時,會在其使用的函數中創建對象$ form和$ error,例如login,register,edit account等。
下面是下載完整登錄腳本的鏈接。下載位於代碼的底部。 http://www.evolt.org/node/60384
我以爲我對OOP有一個很好的理解,或者它是PHP獨有的東西,但任何人都可以簡單地解釋一下哪裏/何時可以創建一個新對象?由於該對象首先在此窗體上使用,因此我想它也必須在這裏創建。任何人都可以向我指出一篇文章的方向,或者可以澄清我的困惑的東西嗎?
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
我引用的特定部分位於表格中,例如在該行中。我知道它是這樣寫的,所以正確類型的錯誤消息將出現在行旁邊,我只是混淆了爲什麼該對象不是在此表單上創建的,即使信息首先在提交表單時從此頁面開始。
<?
include("include/session.php");
?>
<html>
<title>Registration Page</title>
<body>
<?
if($session->logged_in){
echo "<h1>Registered</h1>";
echo "<p>We're sorry <b>$session->username</b>, but you've already registered. "
."<a href=\"main.php\">Main</a>.</p>";
}
else if(isset($_SESSION['regsuccess'])){
/* Registration was successful */
if($_SESSION['regsuccess']){
echo "<h1>Registered!</h1>";
echo "<p>Thank you <b>".$_SESSION['reguname']."</b>, your information has been added to the database, "
."you may now <a href=\"main.php\">log in</a>.</p>";
}
/* Registration failed */
else{
echo "<h1>Registration Failed</h1>";
echo "<p>We're sorry, but an error has occurred and your registration for the username <b>".$_SESSION['reguname']."</b>, "
."could not be completed.<br>Please try again at a later time.</p>";
}
unset($_SESSION['regsuccess']);
unset($_SESSION['reguname']);
}
else{
?>
<h1>Register</h1>
<?
if($form->num_errors > 0){
echo "<td><font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font> </td>";
}
?>
<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" maxlength="50" value="<? echo $form->value("email"); ?>"></td><td><? echo $form->error("email"); ?></td></tr>
<tr><td colspan="2" align="right">
<input type="hidden" name="subjoin" value="1">
<input type="submit" value="Join!"></td></tr>
<tr><td colspan="2" align="left"><a href="main.php">Back to Main</a></td></tr>
</table>
</form>
<?
}
?>
</body>
</html>
沒錯。當使用include()時,就好像包含文件中的所有代碼都被複制並粘貼到包含文件中一樣。 – 2011-12-31 02:36:29
謝謝你們,我想我錯過了$ form = new Form;在session.php頁面上。就像我說的那樣,自從我做了任何編程以來,這已經過去了一段時間,所以我完全放棄了自己的想法。 – user1123815 2011-12-31 02:38:13
剛剛更新了一個表單,我正在通過初始化不同的表單上的對象,它的工作完美。再次感謝! – user1123815 2011-12-31 02:45:41