Is it OOP or as OOP

For a long time reading and testing, but I want to know. Is this the correct PHP OOP code or not

Class User { function Add($Name, $Password){ $sql_str = "INSERT INTO User SET Name = '$Name', Password = '$Password'"; $sql->do_sql($sql_str); } function Del($UserID) { $sql_str = "DELETE FROM User WHERE UserID = '$UserID'"; $sql->do_sql($sql_str); } function Show ($Limit) if ($limit > 0){ $sql_str = "SELECT * FROM User ORDER BY Name LIMIT $Limit"; }else{ $sql_str = "SELECT * FROM User ORDER BY Name"; } $result = $sql->do_sql($sql_str); for ($i = 0; $i < COUNT($result); $i++){ $data[$i]['UserID'] = .... $data[$i]['Name'] = .... } return $Data } } $MyUser = new User; 

And now from the userControl.php file I can control the actions. If I want to do something, I can send the action to an instance of the user class: $MyUser->Add($Name, $Password); Is this approach more like a grouped function than OOP, or is it better to use setters and getters?

If this example is not OOP, then what am I doing wrong and how should this OOP example be done?

Tpx

+6
oop php
source share
4 answers

You are not going to do it right. What you really want to do is a User class representing a single user, with methods that reflect this.

From Wikipedia:

In object-oriented programming, a method is a subroutine that is exclusively associated either with a class (in this case it is called a class method or a static method) or with an object (in this case it is an instance method).

A user object must at least have instance methods that allow it:

  • Download from database
  • Save to database

And a static method for: - Create a user and return the user object.

It should also have a constructor method (__construct (args) in PHP5 or User (args) in PHP4) that is called when the user is created. This should probably mean an identifier or username or something similar so that it can load the right user.

For simplicity, and not just everything for you, imagine a user object with an identifier and a name. This is what the class looks like:

Assuming PHP5:

 class User{ private $id; public $name; public function __construct($id){ $this->load($id); } public function load($id){ // Do a query to load a user and initialize $id and $name. } public function save(){ // Do a query saving $this->id and $this->name to the database. } public static function create($name){ // Do a query to create a user with name $name. } } 

You can load the user with his identifier using new User($id) , or create one specified User::create($name)

At the risk of being figuratively crucified, I would not worry about setters and getters in PHP.

+6
source share

$MyUser->Add($Name, $Password); It looks weird. Try something like this:

 class UserManager { public function add(User $user) { $sql->do_sql("INSERT INTO users (id, name) VALUES (".$user->getId().", ".$user->getName().")"); } public function delete(User $user) { $sql->do_sql("DELETE FROM users WHERE id = ".$user->getId()." LIMIT 1"); } public function show(User $user) { return $sql->do_sql("SELECT * FROM users WHERE id = ".$user->getId()); } } 

and

 class User { private $_id; private $_name; public function getId(){ return $this->_id; } public function getName(){ return $this->_name; } } 

A design pattern that can match, Active Record .

+2
source share

Technically, it is, but you either do not have much code, or your methods will not work. It seems you are not defining $ sql anywhere. Since the beauty of oop really shines when eliminating duplicate code, and you use $ sql in all of your methods, it would be nice to see how you dealt with this. Without complete, working, it is difficult to formulate proposals.

Here is a brief example of what I mean. Since you are not using any PHP functions in PHP, I will stick with PHP4:

 class User { var $sql; function User() { $this->sql = new DatabaseConnection(); } function add($data) { $query = '...query here...'; $this->sql->query($query); } } 

If you want to check out some examples of robust code at the enterprise level, I highly recommend looking at some of the components in the Zend Framework .

0
source share

Thanx! I know that something about OOP is not true to my mind, I need to shake it. Why am I doing this. First I use the template engine. Following user data after this data data file. Something is there actionUser.php:

 $op = ''; IF (ISSET($_REQUEST['op'])){ $op = ADDSLASHES($_REQUEST['op']); } if ($op == 'AddUser'){ $Name = ADDSLASHES($_REQUEST['Name']) $Password = ADDSLASHES($_REQUEST['Password']) $MyUser->Add($Name, $Password) } 

Then send the action to the class user.

User class has litle bit more features

 class User{ private $SQL; public function __construct(){ $this->SQL = SQL::getInstance(); } public Function AddUser ($Name, $Password) { $sql_str ="INSERT INTO USER SET Name = '$Name', Password='$Password'"; $this->SQL->do_sql($sql_str); } public Function DelUser($UserID){ $sql_str = "DELETE FROM User WHERE UserID = '$UserID'"; $sql->do_sql($sql_str); } public Function Login($Login, $Password){ $sql_str = "SELECT * FROM User WHERE Login = '$Login' AND Password = '$Password' "; LIST($sql_result, $sql_count) = $this->SQL->do_sql($sql_str); if ($sql_count == 1){ $_SESSION["UserID"] = $this->SQL->result_strip($sql_result, 0, "AdminUserID"); $_SESSION["Login"] = $this->SQL->result_strip($sql_result, 0, "Login"); $sql_str = "UPDATE User SET LastLogin = NOW()"; $this->SQL->do_sql($sql_str); } } public Function Logout(){ $_SESSION = array(); if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time()-42000, '/'); } session_destroy(); } } 
0
source share

All Articles