Uncaught Error: Call to private method DbOperations::insertData() from context ''

后端 未结 2 1497
一个人的身影
一个人的身影 2021-01-24 05:29

When I\'m trying to insert data trough the database its says \"Uncaught Error: Call to private method DbOperations::insertData() from context \'\'\".

My insert data PHP

相关标签:
2条回答
  • 2021-01-24 06:19

    You cannot call private methods or fields outside class

    Either make the function insertData() public by defining it as follows

    public function insertData() {
        ************
    }
    

    or call it from constructor using

    function __construct($airport, $location, $space , $flight){
        $db = new DbConnect();
    
        $this->con = $db->connect();
    
        $this->insertData($airport, $location, $space , $flight);
    
    }
    

    run it as follows:

    $db = new DbOperations($_POST['airport'], $_POST['location'], $_POST['space'], $_POST['flight']);
    
    0 讨论(0)
  • 2021-01-24 06:25

    You are trying to call a private method outside the class, that's not possible, you can read more about methods visibility on the official PHP methods visibility docs.

    Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inheriting and parent classes. Members declared as private may only be accessed by the class that defines the member.

    You should declare your method as public instead of private if you want to use the method outside the class:

    public function insertData($airport, $location, $space , $flight){
      ... your code
    }
    
    0 讨论(0)
提交回复
热议问题