0
\$\begingroup\$

The client side version in JavaScript is here

This is the server side version of my control module. How does the structure look.

PostScript

Because this is a small prototype/app I have preferences or things I will consider when I will have a working prototype, they are - testing, try/catch/throw handling of errors, advanced abstractions / design patterns, defensive coding against programmer error / malicious users. Auto-Loading, optimization Will add these later if the project matures.

<?php
function __autoload( $class_name ) { include 'class.' . $class_name . '.php'; }
$object_c = new CMachine(); 
$object_c->invoke();

class CMachine
{
    public function invoke()
    {
        $pipe = $this->getPipe();
        switch( $pipe['model'] )
        {
            case 'MUserNew': case 'MUserExist': 
                $model_object = new $pipe['model']( new SDB(), new SUniversals() , new SText( $pipe['page'] ), new SMessage() ); 
                $this->send( $model_object->invoke( $pipe['args'] ) );
                break;
            case 'MUserTry':
                $model_object = new $pipe['model']( new SDB(), new SText( $pipe['page'] ) );
                $test = $model_object->invoke( $pipe['args'] );
                $this->send( $test );
                break;
            case 'MUserAny': case 'MOrb': 
                $model_object = new $pipe['model']( new SDB() );
                $this->send( $model_object->invoke( $pipe['args'] ) );
                break;
            default:
                echo " -> Undefined Module Requested";
        }
    }
    private function send( $string_send )
    {
        echo "|A|" . $string_send;        
    }
    private function getPipe()
    {
        return json_decode( $_POST['pipe'], true );
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Too lazy to write a full answer, but the first thing that jumps out at me is that it looks like your controller is actually a dispatcher and your models are actually controllers. \$\endgroup\$
    – Corbin
    Commented Jun 19, 2012 at 0:18
  • \$\begingroup\$ ...the models hold all my logic...once they are complete they all output a string with the results...the Controller calls the model ( a controller is suppose to call either the view or the model, but I have no views on my server )...and sends the result to the Browser... \$\endgroup\$
    – user7459
    Commented Jun 19, 2012 at 16:26

1 Answer 1

1
\$\begingroup\$

Would separate those includes a bit differently. I'd use a newline after each include then use a double new line when I wanted to section them off. All one line as you are doing, and not even spaces between them, could make someone think that you have only three includes. Speaking of includes. You are loading all of these classes that you aren't going to use. Take a look a lazy initialization. Assuiming you only need to use those classes once in this class, and that is in that switch statement, then you can just include the correct one via case and avoid the overhead of those other unneeded classes.

Would have to agree with Corbin about this looking more like a dispatcher/router. These are okay, just letting you know that what you have isn't a controller.

Would suggest moving the send() method out of the switch. It doesn't really appear to change much and is just causing you to rewrite code. Assign whatever paramters you need to in the switch and just call the method outside of it. To prevent the default case from doing this, since you mentioned wanting to use try/catch, I would throw an error there instead of using echo.

\$\endgroup\$
13
  • \$\begingroup\$ I called it router() at one point...how is a router different from a controller? The send() function provides error checking as all output must go through send() it is concatenated with an |A|, if this is not found on the client an alert is raised... \$\endgroup\$
    – user7459
    Commented Jun 19, 2012 at 16:23
  • \$\begingroup\$ A router's or dispatcher's only job is to choose which model or controller to load and, in my opinion, which session information to pass to it. Such as GET, POST, etc... Think of it as an old fashioned switch board operator. She collects information she needs to forward your call. \$\endgroup\$
    – mseancole
    Commented Jun 19, 2012 at 16:59
  • \$\begingroup\$ codeforest.net/cakephp-from-scratch-basic-principles \$\endgroup\$
    – user7459
    Commented Jun 19, 2012 at 19:30
  • \$\begingroup\$ All the models I've seen show the controller doing this( see link...it shows the dispatcher handling only incoming requests w/ no ties to the model)...perhaps they are combined here...I actually need to remove the switch / case and load dynamically...I will update soon. \$\endgroup\$
    – user7459
    Commented Jun 19, 2012 at 19:32
  • \$\begingroup\$ The good and the bad thing about all design patterns is that everyone adapts them to their own purposes. So the fact that you call a controller, what I call a router, and apparently what Corbin calls a dispatcher, only proves that these are loose terms used to describe an idea and not a strict standard. If you had never heard of a router or dispatcher before and I started explaining my switch board operator analogy, you'd immediately be able to identify it, but as what you think of as a controller. I may not agree on your definition of a controller, but its just a difference in opinions. \$\endgroup\$
    – mseancole
    Commented Jun 19, 2012 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.