简体   繁体   中英

AJAX to a PHP class that extends, the included classes are not passed

I am calling a function via AJAX from a PHP class that extends another class but is not included in that file instead it was included in a different file. To elaborate here's my code:

Client:

$('.username').change(function(){
        var indexOfNumber = $(this).attr('class').indexOf('d');
        var number = $(this).attr('class').substring(Number(indexOfNumber)+1);

        $.ajax({
            url: 'class1.php',
            complete: function(response) {
                alert(response.responseText);
            },
            error: function(){
                alert("error");
            }
        });

        $('.userrole.d'+number).html(number);
    });

Class1:

class class1 extends class2
{
    function class1()
    {
        $this->class2(); << I am getting an error here
    }

The error that I am getting is:

Class 'class2' not found in (location of class1).

class2 is included in the config.php file of the project and thus it's not included again in the files, when I call that file from AJAX it doesn't seem to see the previously included PHP files.

Any way around this?

The only way around it is to require the necessary files in class1.php. Or you could include a file that loads a custom autoloader which can dynamically load files when requesting classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM