开发者

Advice/suggestions for my first project PHP Classes

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)
            {
                $thi开发者_Python百科s->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;    
            }


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜