Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I was wondering if is there any good practices to call method from php class with Javascript, by the way of Ajax.

This is my current "style" to execute it.

(The method in the class are only here for example)

PHP side :

<?php

if(isset($_POST['action']) && $_POST['action'] != null)
{
    extract($_POST);

    if($action)
    {
        $ajaxCommand = new EleveUpdate();

        if(method_exists($ajaxCommand, $action))
        {
            $reponse = call_user_func(array($ajaxCommand, $action),$_POST);
            echo $reponse;
            exit(0);
        }
        else 
        {
            throw new Exception("Cette méthode n'existe pas");
        }
    }
}
else
{
    echo 'Cette action n\'est pas autorisée';
    return false;
}

class EleveUpdate
{

    public function __construct()
    {

    }

    public function testfunct($data)
    {
        echo $data['eleve'];
    }
}

Javascript side:

$(document).ready(function() {
    $.ajax({
        url: 'eleveupdate.php',
        type: 'POST',
        data: {
          action: "testfunct",
          eleve: 1
        },
        beforeSend: function()
        {
            loading(true);
        },
        error: function()
        {
            console.log('error');
        },
        success: function(data) 
        {
            loading(false);
            console.log(data);
        }
    });
});

The problem is that the isset $_POST is always in my class, I'm pretty sure this is not the right way to do it so, I'm here to found help about it.

Thanks you in advance

Simon

share|improve this question
What do you think it should happen, and what happens instead? Moreover, try to move the class declaration on top. – gd1 Feb 17 at 22:01
The idea is to have a more "elegant" way to do it. This method works perfectly but, I don't know if I want to use it with like that. Everything is returned like I want but I want to find the "good practice" way :) – Simon Feb 17 at 22:13
The only 'problem' I see is that someone can call an arbitrary method of your class from outside. If your class only contains 'safe' methods, there is no issue with that. Moreover, I don't like the use of extract() for $_POST variables. – gd1 Feb 17 at 22:17
Is that a "bad way" to use extract in that case ? (This is for my own information :)) – Simon Feb 17 at 22:20
2  
Imagine a scenario where you use a variable to mean someone has special abilities like 'admin' and your code was saying if ($admin == true) blah blah. By using extract() someone could put admin=1 in the AJAX call and become an admin because extract took the value from the URL and replaced a local variable with it. – Crossphire Development Feb 17 at 22:32
show 2 more comments

1 Answer

The problem is it's not easy to call scope-namings based on a string-request. You can use something called LAMBDA, or anonymous functions. This way you can store an array of strings with a LAMBDA attached to them. Like this:

<?php
class EleveUpdate {
    private $funcs = array();

    function __construct($which_function) {
        $funcs['testfunct'] = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');

        $funcs[$which_function];
    }
}
?>

See more info here: http://www.php.net/manual/en/function.create-function.php

You probably won't even need the Class anymore with this method, but just showing.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.