Tips / suggestions for my first project PHP classes

Any advice is appreciated! I have a very limited understanding of php classes, but below is my starting point for the route I would like to take. The code is a reflection of what I see in my head and how I would like to do business. Does my code even look normal, or am I not in the database?

What are your thoughts, how could you achieve a task like form-> validate-> insertquery-> sendmail-> return messages and errors?

Please try to make your answers simple enough for me to digest, as for me, an understanding of what is happening, and not just copy / paste.

Best regards, Phil.

Note. This is the basic structure, no complete code added.

<?php //======================================= //class.logging.php //======================================== class logging { public $data = array(); public $errors = array(); function __construct() { array_pop($_POST); $this->data =($this->_logging)? is_isset(filterStr($_POST) : ''; foreach($this->data as $key=> $value) { $this->data[$key] = $value; } //print_r($this->data); de-bugging } public function is_isset($str) { if(isset($str)) ? true: false; } public function filterStr($str) { return preg_match(do somthing, $str); } public function validate_post() { try { if(!is_numeric($data['cardID'])) ? throw new Exception('CardID must be numeric!') : continue; } catch (Exception $e) { return $errors = $e->getCode(); } } public function showErrors() { foreach($errors as $error => $err) { print('<div class="notok"></div><br />'); } } public function insertQ() { $query = ""; } } //======================================= //Usercp.php //======================================== if(isset($_GET['mode'])) { $mode = $_GET['mode']; } else { $mode = 'usercp'; } switch($mode) { case 'usercp': echo 'Welcome to the User Control Panel'; break; case 'logging': require_once 'class.logging.php'; $logger = new logging(); if(isset($_POST['submit']) { if($logger->validate_post === true) { $logger->insertQ(); require_once '/scripts/PHPMailer/class.phpmailer.php'; $mailer = new PHPMailer(); $mailer->PHPMailer(); } else { echo ''.$logger->showErrors.''; } } else { echo ' <form action="'.$_SERVER['PHP_SELF'].'?mode=logging" method="post"> </form> '; } break; case 'user_logout': // do somthing break; case 'user_settings': // do somthing break; ?> 

I decided to use this method to return errors, and not to print in the method, thanks for the advice of Igor!

 catch (Exception $e) { $this->errors[] = $e->getMessage(); #ERROR DE_BUGGING ONLY================================ #print('<pre>'); #print_r($this->errors); #print('</pre>'); #===================================================== } if($this->errors) { return false; } else { return true; } 
+4
source share
2 answers

It looks like you have a decent understanding of OOP code. I see declared public vars and even attempts / catches, although I would say do not forget the β€œpublic” visibility keyword before the β€œ__construct () function” - it is not absolutely necessary, but it supports good encoding methods.

In addition, I would say that everything you do here has been written, debugged and fixed, and proven products worthy of each of the many PHP frameworks already exist. The specific task you mentioned, "form-> validate-> insertquery-> sendmail-> returns messages and errors" is so incredibly simple in the Zend Framework, in my sample. And I would suggest that the same is true for Symphony, Solar, Cake, etc.

Do yourself a favor and stop coding what has already been encoded. Explore a community-based structure, regular updates, and well-written, complete documentation. Again, I recommend the Zend Framework .

+3
source

First tip that comes to mind: Separate logic from presentation. You can start by using some kind of template engine, such as Smarty . If you mix it all up, soon it will be a huge dump.

Also try including class definitions from separate files, and as a next step, I would recommend using some template, such as Model-View-Controller, to separate the models from the logic.

This is what I can think of without going too deep into the code.

+1
source

Source: https://habr.com/ru/post/1309666/


All Articles