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 6180549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:51:49+00:00 2026-05-24T00:51:49+00:00

I am trying to extend the PHP5 MySQLi Library class mysqli_ls extends mysqli The

  • 0

I am trying to extend the PHP5 MySQLi Library

class mysqli_ls extends mysqli

The reason I am extending the class is to add functionality and features I require that are not included by default. For the functions I create I would like to handle errors in exactly the same way as i would any of the native functions..

I would like the person using the class not have to determine if the functions are native or bespoke.

Normal Function

if ( $_mysqli->query($sql) === false ) die($_mysqli->error);

Required Bespoke Function Example

if ( $_mysqli->run_config() === false ) die($_mysqli->error);

Class

Within my class mysqli_ls that is extending mysqli, i have the following functions which i hoped would set the error message within the parent mysqli class..

   public function run_config()
   {
      // Open Debug FH
      if ($this->config['debug_log'] === TRUE)
      {
         if ($this->fh_debug = fopen($this->config['debug_log_path'], 'a') === FALSE) 
         {
            $this->handle_error('Could not open Debug file handle','200',__METHOD__);
            return $this->E_ERROR;
         }
      }
  }



private function handle_error($errormsg,$errorno,$method)
   {
      $this->errorno = $errorno;
      $this->error   = $errormsg;

      if ($this->config['debug_log'] === TRUE)
      {
         $msg = "($errorno) $errormsg";                              
         $this->write_log('error', $method, $msg);
      }

      return $this->E_OK;
   }

I have tried the above method which errors out with:
PHP Fatal error: mysqli_ls::handle_error(): Cannot write property

and the static call syntax which results in a syntax error as seen below:

parent::error
parent::errorno 

I would like the users of the class to have the default MySQLI functionality and the extended functionality seamlessly without having to use different methods to identify errors.

Thanks in advance for any advise or input given..

EDIT: Adding the full class

class mysqli_ls extends mysqli
{
   private $config = array
   (
      'config_load'     => FALSE,
      'config_path'     => 'mysqli_ls.database.ini',

      'debug_log'       => FALSE,
      'debug_log_path'  => '/tmp/mysqli_ls.debug.log',

      'query_log'       => FALSE,
      'query_log_path'  => '/tmp/mysqli_ls.debug.log',

      'log_date_format' => 'Y-m-d H:i:s'
   );

   // Expected fields for the autoload file
   private $db_fields = array('hostname','username','password','database','port');

   // File Handles
   private $fh_debug  = FALSE;
   private $fh_query  = FALSE;
   private $fh_config = FALSE;

   // Return presets
   private $E_OK    = TRUE;
   private $E_ERROR = FALSE;

   // Existing database connections
   private $db_existing = array();

   public $error;
   public $errorno;

   ## ----------
   public function __construct()
   {
      if (!function_exists('mysqli_connect')) $this->handle_error('MySQLI is not installed','100');
   }

   ## ----------
   public function run_config()
   {
      // Testing error handling
      $this->handle_error('Could not open Debug file handle','200',__METHOD__);

      // Open Debug FH
      if ($this->config['debug_log'] === TRUE)
      {
         if ($this->fh_debug = fopen($this->config['debug_log_path'], 'a') === FALSE) 
         {
            $this->handle_error('Could not open Debug file handle','200',__METHOD__);
            return $this->E_ERROR;
         }
      }

      // Open Query FH
      if ($this->config['query_log'] === TRUE) 
      {
         if ($this->fh_query = fopen($this->config['query_log_path'], 'a') === FALSE)
         {
            $this->handle_error('Could not open Debug file handle','210');            
            return $this->E_ERROR;
         }
      }

      // Load database INI file
      if ($this->config['config_load'] === TRUE)
      {
         if ( ($this->db_existing = parse_ini_file($this->config['config_path'], TRUE)) === FALSE )
         {
            $this->handle_error('Could not parse the database ini file','220'); 
            return $this->E_ERROR;
         }
      }

      // Check for format of the loaded ini file
      foreach($this->db_existing as $name => $row)
      {
         foreach($this->db_fields as $field)
         {
            if ( !isset($row[$field]) ) 
            {
               $this->handle_error("Missing field ($field) in the config array for element ($name)",'230'); 
               return $this->E_ERROR;
            }
         } // END foreach
      } // END foreach

      return $this->E_OK;
   }

   ## ----------
   public function set_config($key, $value)
   {
      if ( !isset($this->config[$key]) ) 
      {
         $this->handle_error('Configuration variable ($key) does not exist','300');
         return $this->E_ERROR;
      }

      $this->config[$key] = $value;
      return $this->E_OK;
   }

   ## ----------
   public function get_config()
   {
      return array_merge($this->config, $this->db_existing);
   }

   ## ----------
   private function write_log($type,$method,$msg)
   {
      $msg = date($this->config['log_date_format']) ."\t". $type ."\t". $method ."\t". $msg ."\n";

      switch($type)
      {
         case 'error':
            fwrite($this->fh_debug, $msg);
         break;

         case 'debug':
            fwrite($this->fh_debug, $msg);
         break;

         case 'query':
            fwrite($this->fh_query, $msg);
         break;

         default:
            return $this->E_ERROR;
         break;      
      }
   }

   ## ----------
   private function handle_error($errormsg,$errorno,$method)
   {
      $this->errorno = $errorno;
      $this->error   = $errormsg;

      if ($this->config['debug_log'] === TRUE)
      {
         $msg = "($errorno) $errormsg";                              
         $this->write_log('error', $method, $msg);
      }

      return $this->E_OK;
   }

   ## ----------
   ## ----------
   ## ----------
   ## ----------      

} // END Class

TEST Script Calling the Class

#!/usr/bin/php
<?php

require('mysqli_ls.class.php');

try
{
   $_mysqli = new mysqli_ls;
}
catch (Exception $e)
{
  print "\n\n". $e->getMessage() ."\n\n";
   print_r($e);
}

$_mysqli->set_config('debug_log',TRUE);
$_mysqli->set_config('query_log',TRUE);
$_mysqli->set_config('config_load',TRUE);

$_mysqli->run_config();
print_r($_mysqli->get_config());

?>

EDIT: So it looks like the error variables in the MySQLi class might be readonly..Is there another way around this issue? Im thinking maybe have my own error function that checks the parent class AND my own variables.

  • 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-24T00:51:50+00:00Added an answer on May 24, 2026 at 12:51 am

    Using the __get magic function, you can do something which will check if the parent’s error variable is set. Something like that:

    class mysqli_ls extends mysqli{
      public $error;
      public $errorno;
      public $connect_error;
      public $connect_errorno;
    
      function __get($key){
        if(in_array($key, array('error', 'errorno', 'connect_error', 'connect_errorno'))){
          $parentValue = parent::$key;
          if(empty($parentValue)){
            return $this->$key;
          }
          return $parentValue;
        }
      }
    }
    

    Little test script:

    <?php
    $test = @new mysqli2('localhost', 'badlogin', 'whatisapassword', 'anonexistentdb');
    echo 'test : '.$test->connect_error;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to extend the mysqli class to make a helper class that
I'm trying to extend the functionality of an ASP.net application that I'm working with.
I am trying to extend the TcpClient class in the oscP5 library for Processing.
I am trying to extend the Bitmap class so that I can apply my
I'm trying to extend a library that uses generics in entities and entities manager,
I am trying to extend BitmapDrawable public class CenterClampedBitmapDrawable extends BitmapDrawable I want to
I'm trying to extend Clojure to add durability to refs in a way that
I'm trying to extend some base classes in Python: class xlist (list): def len(self):
I am trying to extend the partial classes that the entity framework creates so
I'm trying to extend the Method class along the lines of: irb(main):008:0> class A

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.