5

I'm going to write a booking website using php and ajax and I really can't figure how to mix these two tools with a strict object oriented design.

I was used to make a call using ajax to a php web page that returns the right set of values (string, xml, json) in a procedural way.

With object oriented programming how is it supposed to work?

The simplest solution that i can think is to call through ajax a php page that should only instantiate a new object of the right class and then make an echo on the result of a simple call with the data received but this doesn't look very oo...

For example to implement the register function I should make an ajax call to a register.php web page that, in turn, will instantiate a new Registration object r and then simply calls r.register() with the right data.

Is there a better solution to this problem?

I want specify that I can't use any php framework because it's a didactic project and I have this rule that I should respect.

Another specification: I've read a lot of tutorials that describe how to write your own mvc framework but doing this seems to be an overkill for my problem.

Thank you for your help, every idea will be appreciated.

2 Answers 2

1

As you already said you don't really need a PHP framework and don't need to build your own MVC implementation (especially if you are e.g. working with JSON or XML). Basically you are pretty free on how to do your OO model, so your idea is not necessarily wrong.

Some OO anti patterns I have seen people using in PHP:

  • Use global variables in classes
  • Create classes without member variables resulting in method calls being the same as in a produral style
  • Directly accessing $_GET, $_POST etc. in a class
  • Echoing html output (imho this should be done in view templates)

An example for what you might want to do for the registering process processing some $_POST variables and returning a JSON success message:

<?php

class Registration
{
    private $_data;

    public function __construct($registrationdata)
    {
        $this->_data = $registrationdata;
    }

    public function validate()
    {
        // ...
    }

    public function register()
    {
        // ...
        if($this->validate())
            return array("registered" => true, "username" => $this->_data["username"], 
                "message" => "Thank you for registering");
        else
            return array("registered" => false, "username" => $this->_data["username"],
                "message" => "Duplicate username");
    }
}

$reg = new Registration($_POST);
echo json_encode($reg->register());

?>
Sign up to request clarification or add additional context in comments.

4 Comments

That's exactly what i was thinking. Maybe session management could be a bit problematic in this way because it becomes a cross cutting concern.
I think you won't be able to entirely get rid of having to deal with cross cutting concerns. As for security and session management the MVC pattern works quite well because you can always do it in your controller before anything is done.
But I think that the model of code you've posted needs to be changed a bit. Before the input validation (obviously not in the registration part) I think I should check the session validity and so almost every controller object constructor will need to take a session argument. Am I right?
The controllers yes, but design wise I would let the Registration class assume that there has been dealt with session and security management already.
1

There is no reason to create any classes if all you are doing is calling a couple of unrelated stateless php functions.

1 Comment

I have to manage sessions and databases, i presume it will be simpler if I use a full oo approach.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.