oop - Who defines Mysqli class and its methods / properties in PHP


I´m very new in OOP and trying to clarify a few things.

  1. This is a simple class with some properties and at the end of the code I´m calling new instance $me from class. I understand this.

    // The code below creates the class
    class Person {
        // Creating some properties (variables tied to an object)
        public $isAlive = true;
        public $firstname;
        public $lastname;
        public $age;
        // Assigning the values
        public function __construct($firstname, $lastname, $age) {
          $this->firstname = $firstname;
          $this->lastname = $lastname;
          $this->age = $age;
        // Creating a method (function tied to an object)
        public function greet() {
          return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
    // Creating a new person called "boring 12345", who is 12345 years old ;-)
    $me = new Person('boring', '12345', 12345);
    // Printing out, what the greet method returns
    echo $me->greet(); 
  2. By Mysqli I'm starting right with new instance definition. Where is the class definition part? I found this link and there are many properties and methods but I can´t find properties (I believe that they are properties) $servername, $username etc. Who defines them and who defines their order in which I have to type them in the new mysqli ?

     $conn = new mysqli($servername, $username, $password, $dbname);

Summary: if I understand right, Mysqli is something like predefined class which is stored somewhere in PHP but I´m still missing properties as a $servername, $username etc.




PHP comes with a wealth of so called "extensions". Kind of plugins to the PHP engine, which provide such definitions and implementations of classes, constants and functions. Such extensions are (usually) written in the C language and have to be compiled, so you cannot easily modify them. Extensions are often bundled with a PHP distribution, many are activated in standard installations.

For themysqli class there indeed is amysqli extension. Such extension has to be loaded into your PHP engine, only then you can use what features are provided by the extension. PHPs configuration files control which extensions are loaded upon startup of the PHP engine.

Two other concepts exist, I will add them for the sake of completeness. Though as saidmysqli is provided by an extension.

Some packages are also implemented in PHP itself and distributed as such, for example the "PEAR" classes. Those can be installed on a server and you can use them, but you have to include one central file from the package by means of arequire() call or similar.

Then there is the great concept of "auto loading" which allows you to setup your stuff such that PHP can include PHP code automatically when required. That is typically done based on a strict naming scheme you use for your classes. The class name is then parsed, from that information the file to load is identified. PHP then loads the file automatically and just in time in background. thus the name "auto loading". Very convenient for bigger projects.

Last there is one the "auto prepend" strategy sometimes used for such purposes: you can configure PHP such that it always automatically loads one or more files prior to starting to interpret the actual file requested by the http request. This can be done inside the php configuration or the http host configuration. Comes in handy to magically load things like database credentials.

But as said before:mysqli is a PHP extension and typically bundled and activated with every standard installation.




$servername and $username are not properties. That's function parameters, just like in yourPerson::__construct().

If you send variables into your person class in the same random order, you will get the same weird results as well, like

"Hello, my name is 33 Joe. Nice to meet you! :-)";

It has nothing to do with mysqli, properties or OOP in general. That's just function parameters which function will use in order that has been predefined in the function declaration.




  • mysqli class is a part ofmysqli PHP extension. For all your purposes, you can think about it as a built-in class.
  • mysqli comes with PHP. It is a bundled extension. The source codes are located here: https://github.com/php/php-src/tree/master/ext/mysqli
  • To answer your question "Who defines them [properties] and who defines their order in which I have to type them in the new mysqli?". The author of themysqli PHP extension designed API for MySQL and this is how he/she decided it should be:$conn = new mysqli($servername, $username, $password, $dbname);
  • Properties can be accessed as a property of any other class - for example$mysqli->affected_rows; (http://php.net/manual/en/mysqli.affected-rows.php)

People are also looking for solutions to the problem: php - Array if contains value, unset


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.