Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6006665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:34:20+00:00 2026-05-23T01:34:20+00:00

i having problem understanding what is wrong with my inheritance. I just can not

  • 0

i having problem understanding what is wrong with my inheritance. I just can not call most of the function of the parent. below is the function i am stuck.

class MySQLi_DB  extends mysqli {
    private static $_instance = null;
    private function __construct($db="test",$host="localhost", $user="root", $pass="") 
    {

        parent::__construct($host, $user, $pass, $db);
        if (mysqli_connect_error()) {
            die('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
    }
 static public function getDB()
    {
        if(self::$_instance == null)
        {
            self::$_instance = new MySQLi_DB();
        }
        return self::$_instance;

    }


public function insert($table,$data)
        {
           $sql = $this->getQuery($table,$data);
           print $sql;
        }

        public function getQuery($table, $inserts) 
        {
            $lambda = function($value){
                return $this->real_escape_string($value);
            };

            $values = array_map($lambda,$inserts);
            $keys = array_keys($inserts);

            return 'INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')';
        }

I am having problem calling real_escape_string() function . i have tried using $this->real_escape_string() but i just have to see this error

Fatal error: Using $this when not in
object context in
D:\wamp\www\Driver\MySQLi_DB.php on
line 49

ok may be that could be limitation of lambda function but i have tried it other way by declaring a callback for array_map that also doesn’t allow me to call real_escape_string.

i am calling the function as below.

require_once 'MySQLi_DB.php';
        $db = MySQLi_DB::getDB();

        $insert_data = array("cmsId"=>444,"pageName"=>"New Insert");
        $db->insert("cms",$insert_data);

Please point out where i am doing wrong and what could be the best way to do it. thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:34:21+00:00Added an answer on May 23, 2026 at 1:34 am

    You can’t use the $this keyword into a lamba function as the scope of the function does not extend to the object whose method contains the lambda

    Try a different approach:

     public function getQuery($table, $inserts) 
     {
          $values = array_map(array($this, 'real_escape_string'),$inserts);
          $keys = array_keys($inserts);
    
          return sprintf('INSERT INTO %s (`%s`) VALUES ("%s")',
               $table,
               implode('`,`', $keys),
               implode('","', $values)
          );
        }
    

    Addendum

    If real_escape_string is not a method of your object but the standard msqli method you should change the line

     $values = array_map(array($this, 'real_escape_string'),$inserts);
    

    to

     $values = array_map(array(self::$_instance, 'real_escape_string'),$inserts);
    

    to call the method real_escape string.

    The array array(self::$_instance, 'real_escape_string') is a callback array and it is used when you want to call an object method.

    The php manual states

    A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1

    and self::$_instance is the mysqli instance you want to call

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem understanding Java generics and I've simplified to this example class
I'm having a problem understanding what I'm doing wrong here. I have the code
I'm having a problem understanding the shift/reduce confict for a grammar that I know
I'm having a little bit of trouble understanding what the problem is here. I
I'm new to Ruby, so I'm having some trouble understanding this weird exception problem
Having a problem getting a TreeView control to display node images. The code below
Having a problem trying to create a function, as part of a BizTalk helper
We were having a problem with our build server not checking out modifications from
Having problem understanding Delegate error. Error says expression expected, How do I fix? Thanks
I am having a problem understanding how polymorphism works when using generics. As an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.