2016-01-15 69 views
-2

我正试图学习面向对象的编程,我想将这些代码变成这样。到目前为止,我已经从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*/ 
+3

,我不知道你是否会同意,但我发现它很有用,当我得知OOP到谷歌搜索随机PHP类,然后从gitHub等读取其他人的类,查看使用示例并查看它是如何工作的。通常“学习者”类太简单了,不能给真实生活中的实际使用提供建议,所以请下载一些gitHub类并尝试使用它们,然后尝试并自己制作.....祝你好运 – Martin

+0

谢谢,我会那么做 – Alei

+0

也尝试使用GET/SET方法在类中存储数据:http://stackoverflow.com/questions/10616866/is-it-worth-making-get-and-set-methods-in-oop – Martin

回答

0

class User { 
public $nameStore, $fName, $lName, $email; 

    public function __construct ($fName, $lName, $email) { 

     $this->$fN = $fName; 
     $this->$lN = $lName; 
     $this->$eN = $email; 
    } 

我会打破这种成更具体的部分,如获取和设置您正试图在类存储每个值:

class User { 
    private $fName, $lName, $email; 

    public function set_firstname($fname){ 
     $this->fName = $fname; 
} 

    public function set_surname($lName){ 
     $this->lName = $lName; 
} 

    public function set_email($email){ 
     $this->email = $email; 
} 
    public function get_email(){ 
    return $this->email; 
} 
    public function get_fname(){ 
    return $this->fName; 
} 
    public function get_surname(){ 
    return $this->lName; 
} 

然后当你创建这个类时,你可以单独添加和返回每个值,而不是强迫自己去做一次完成。这更灵活。但是你可以在创建类的使用__construct相似还添加值,以及如果你愿意,你有什么不已:

public function __construct ($fName = null, $lName = null, $email = null) { 
     if(!empty($fName)){ 
      $this->set_firstname($fName); 
     } 
     if(!empty($lName)){ 
      $this->set_surname($lName); 
     } 
     if(filter_var($email, FILTER_VALIDATE_EMAIL) !== false){ 
      $this->set_email($email); 
     } 
    } 

这样做是对每个非空值它运行相应的SET方法。在保存之前还要检查电子邮件的值是否有效。如果没有值传递给该类,那么它不会保存任何内容。

你之类的设立是不正确,首先你需要包括类文件到工作PHP所以在你的页面加载的顶部:

include "path/to/users.class.php"; 

并正确初始化类:

$userClassInstance = new User($firstName,$lastName,$emailUser); 

当上面的行运行时,您将有一个用户对象包含引用为$userClassInstance三个变量。你可以做var_dump($userClassInstance);

要小心,因为你的代码具有newUser作为一行,并且在构造语句中也有不正确数量的变量。通常,页面中的所有functions都应放置在适当的类中,因此,您可以将所有字符串管理函数(如errorCheck())放入Users类中,以检查给定的值,然后将它们分配给类中的变量。

最后,查看存储的变量,那么你会怎么做:

print $userClassInstance->get_fname(); //will outout the value of the class $fName 
相关问题