views:

143

answers:

2

Hi guys, Any advice is welcome! 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 go about business. Does my code even look ok, or am I way off base?

What are your thoughts, how would you go about achieving such a task as form->validate->insertquery->sendmail->return messages and errors?

Please try and keep your answers simple enough for me to digest as for me its about understanding whats going on and not just a copy/paste job.

Kindest regards, Phil.

Note: This is a base structure only, 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 have decided to use this method for returning errors rather than print them in the method, thanks for the advice 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;    
            }
+1  A: 

First advice that comes to mind: Separate logic from presentation. You can start by using some template engine like Smarty. If you will keep it all mixed up, soon it will be a huge dump.

Also try to include class definitions from separate files, and as a next step I would recommend adopting some pattern like Model-View-Controller to separate models from logic.

That's what I can think of without digging too deep into the code.

Igor Zinov'yev
Hi Igor, thanks for your reponce.Im building an application for a community to help people log data, which I will be releasing as CC so I guess frameworks are out of the question.I will look into MVC model in more detail, thanks.
Philip
Smarty is open source, shouldn't be a problem for CC release, I wouldn't think.
Geoff
I will look into that, thanks Geoff!
Philip
Question for IZ: the code seems to reflect awareness of the need to separate logic and presentation. Aside from avoiding use of echo, I wouldn't know how to improve it. I'm not arguing against Smarty, but to help my ignorance, is there anything specific that would concern you?
Smandoli
Smandoli: I'm not saying that you should avoid using echo, my point is that you shouldn't include raw markup into models. For example, we see here that `logging` class here outputs errors inside its own method using: `print('<div class="notok"></div><br />');`I would rather collect errors in a class property and then access it from a file where all my markup is stored. And it does not necessarily have to be Smarty, one could simply use `<?php foreach($log->errors as $error): ?><div class="notok"><?=$error['message']?></div><br /><?php endforeach; ?>`
Igor Zinov'yev
Thanks IZ! Very helpful clarification, and advice I can prolly apply. +1
Smandoli
I take everybody's advice on board, thanks alot guys!
Philip
+3  A: 

It looks like you have a decent understanding of OOP code. I see declared public vars and even try/catches, though I'd say don't forget the "public" visibility keyword in front of "function __construct()"—not absolutely necessary, but it keeps with good coding practices.

Further, I would say that everything you are doing here has been written, debugged, and fixed, and proven production worthy already by each of the dozens of PHP frameworks out there. The specific task you mentioned, "form->validate->insertquery->sendmail->return messages and errors" is so incredibly easy with Zend Framework, my framework of choice. And I would imagine the same is true for Symphony, Solar, Cake, etc.

Do yourself a favor and stop coding what has been coded already. Learn a framework that has a community, regular updates, and well-written thorough documentation. Again, I recommend Zend Framework.

talentedmrjones
+1 for Zend Framework. They have a very good coding standard that helps to write good code.
Igor Zinov'yev