We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Is this a good practice

Hi guys,

I just want to ask you guys if what im doing is a good practice in coding, I'm referencing the $this->request as self::$hit on my basecontroller and using parent::$hit on my homecontroller so that i dont have to use $this->request everytime i get the value of fields in a form.

BaseController.php


class BaseController extends Controller
{
    public static $hit;

    public function initialize()
    {

        /* reference the request */
        self::$hit = $this->request;
    }
}

HomeController.php


class HomeController extends BaseController
{

    private static $username, $fname, $lname, $email, $password;

    public function initialize()
    {
        parent::initialize();
    }

    public function RFvalidationAction($form)
    {
        switch($form)
        {
            case ($form->isValid($_POST)):

                $count_users        = TblUser::find();

                self::$fname            = parent::$hit->getPost('fname', ['string', 'striptags', 'trim']);
                self::$lname            = parent::$hit->getPost('lname', ['string', 'striptags', 'trim']);

            break;

            deafult:

            break;
        }
    }
}


58.4k

I think not necessary, if you want to short this->request in HomeController

    public function RFvalidationAction($form)
    {
       $request = $this->request;
       switch($form)
        {
            case ($form->isValid($_POST)):

                $count_users        = TblUser::find();

                self::$fname            = $request->getPost('fname', ['string', 'striptags', 'trim']);
                self::$lname            = $request->getPost('lname', ['string', 'striptags', 'trim']);

            break;

            deafult:

            break;
        }


21.5k
Accepted
answer

What are you trying to optimize?

parent::$hit vs $this->request? It's 12 vs 14 chars?

So I don't think it is a good practice, IMHO.

hahahhaha yeah right @Jimmy Chandra :)



58.4k

Hey @Steven

You have tips phalcon? if you have share it on https://phalcontip.com

wow @Thien thanks this is nice, i didnt know that phalcon have this tips page or what ever u call it :) once I have done something I'll share it :)