我正试图学习面向对象的编程,我想将这些代码变成这样。到目前为止,我已经从google和这里获得了一些知识,特别是http://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762。我该如何开始将此代码转换为OOP PHP?
我的理解是,我需要包含一组指令的类,这些指令可以用于这些类之外的通用对象。
到目前为止,我的想法是设置一个用户类,其中存储名称(来自HTML/PHP表单)。
class User {
public $nameStore, $fName, $lName, $email;
public function __construct ($fName, $lName, $email) {
$this->$fN = $fName;
$this->$lN = $lName;
$this->$eN = $email;
}
像上面那样^。但是我仍然对我的代码的其他说明应该去哪里感到困惑。这是我需要最多帮助的地方。从我读过的内容来看,它并没有帮助我全面掌握我需要做的事情。如果有人能帮助我开始如何将代码转换为OOP类型,我将非常感激。谢谢!
以下是我想要转换为OOP的程序代码。
<?php
session_start();
$formNames = $_POST['names'];
$active = (isset($_POST['activate'])) ? $_POST['activate'] : false;
//checks if activate checkbox is being used
$email = '@grantle.com';
$fullnames = explode(", ", $_POST['names']);
if ($active == true) {
$active = '1';
//sets activate checkbox to '1' if it has been selected
}
/*----------------------Function to Insert User-------------------------*/
function newUser($firstName,$lastName,$emailUser,$active,$conn){
//a function to insert a user into a database is here
}
//newUser function enters names in database
/*-------------------------End Function to Insert User--------------------*/
/*-----------------------Function for Errors------------------------------*/
function errorCheck($formNames, $nameSplit, $fullname){
$isValid = false;
if (empty($fullname)) {
$_SESSION['error'][] = '<br><br> Error: Name Missing Here: '.$fullname.'<br><br>';
} elseif (empty($nameSplit[0])) {
$_SESSION['error'][] = '<br><br> Error: First Name Missing Here: '.$fullname.'<br><br>';
} elseif (empty($nameSplit[1])) {
$_SESSION['error'][] = '<br><br> Error: Last Name Missing Here: '.$fullname.'<br><br>';
} elseif (preg_match('/[^A-Za-z, ]/', $fullname)) {
$_SESSION['error'][] = '<br><br> Error: Illegal Character Found in: '.$fullname.'<br><br>';
} else {
$isValid = true;
}
return $isValid;
}
//errorCheck function tests for errors in names and stops them from being entered in the
//database if there are errors in the name. Allows good names to go through
/*-----------------------------End Function for Errors---------------------*/
/*--------------------------Function for Redirect--------------------------*/
function redirect($url){
$string = '<script type="text/javascript">';
$string .= 'window.location = "' .$url. '"';
$string .= '</script>';
echo $string;
}
//redirect function uses a javascript script to redirect user because headers have already been sent.
/*-----------------------------End Function for Redirect-----------------------*/
// Connect to database
I connect to the database here//
// Initialize empty error array
$_SESSION['error'] = array();
foreach ($fullnames as $fullname) {
$nameSplit = explode(" ", $fullname);
//I open the database here
//opens the database
if (errorCheck($formNames, $nameSplit, $fullname)) {
$firstName = $nameSplit[0];//sets first part of name to first name
$lastName = $nameSplit[1];//sets second part of name to last name
$emailUser = $nameSplit[0].$email;//sets first part and adds email extension
newUser($firstName,$lastName,$emailUser,$active,$conn);//do this BELOW only for names that have no errors
}//ends if of errorCheck
}//ends fullnames foreach
if (count($_SESSION['error']) == 0) {
redirect('viewAll.php');
} else {
redirect('form.php');
}
/*Redirects to viewAll page only once and as long as no errors have been found*/
,我不知道你是否会同意,但我发现它很有用,当我得知OOP到谷歌搜索随机PHP类,然后从gitHub等读取其他人的类,查看使用示例并查看它是如何工作的。通常“学习者”类太简单了,不能给真实生活中的实际使用提供建议,所以请下载一些gitHub类并尝试使用它们,然后尝试并自己制作.....祝你好运 – Martin
谢谢,我会那么做 – Alei
也尝试使用GET/SET方法在类中存储数据:http://stackoverflow.com/questions/10616866/is-it-worth-making-get-and-set-methods-in-oop – Martin