Php-mysql Course

The Constructor method is a special type of function called __construct within the class body.
To declare /create a constructor method, use the __construct name (begins with two underscore "__").
This method is always "public" even if this attribute is not specified.
The difference from the other functions is that a constructor method is automatically invoked when an object is created.


- Here's a new version of the SiteClas class (created in the previous lesson) which has declared a constructor method, also it has two properties and two methods.
<?php
// SiteClas class
class SiteClas {
  public $site = 'coursesweb.net/';         // public property
  private $category = 'php-mysq/';             // private property

  // Define constructor
  public function __construct($name) {
    // outputs a message, including the "site" property
    echo 'Welcome '. $name. ' on '. $this->site. '<br />';
    echo $this->Mesaj();       // adds and returns the Mesaj() method
  }

  // protected method
  protected function Mesaj() {
    // return a message includind the 'category' property
    return 'Web site category: '. $this->category;
  }

  // public method, receive an argument
  public function pages($pag) {
    // output the URL address consists of the value of the two properties and its argument
    echo '<br />'. $this->site. $this->category. $pag;
  }
}
?>
The constructor method uses the pseudo-variable $this to access the elements of its class (here "site" property and the Mesaj() method).
Because the constructor method has a parameter ($name) [you can add more parameters or none, as any function], when it creates an object instance of the class you must add and an argument too.

-The following example includes the SiteClas class (saved in the "class.SiteClas.php" file), and instantiate an object.
<?php
include('class.SiteClas.php');        // Include SiteClass class

// create an object of SiteClass, with one argument
$objSite = new SiteClas('Marius');
?>
When the $objSite is set, the constructor method receives the argument ('Marius') and executes its code.
Output:
Welcome Marius on https://coursesweb.net/
Web site ategory: php-mysq/

Setting optional parameters

If the constructor method is defined with a number of parameters, without an initial value, when the object instance is created, the class must be called with the same number of arguments, otherwise return error.
For example, the constructor method defined in the previous example has one parameter ($name), if you create an instance of SiteClas without argument, like:
          $obj = new SiteClass();
will generate an error like:
Warning: Missing argument 1 for SiteClas::__construct(), called in ...

You can declare parameters with an initial value, so the arguments for them become optional.
  - Example (a class named Test which has a constructor with an optionl argument ( $name='You' ) ):
<?php
class Test {
  // Constructor (a parameter with default value)
  public function __construct($name='You') {
    echo '<br />Hy '. $name;
  }
}
?>
- If the instance of the Test class is created without argument, the constructor method will use the "$name" parameter with its initial value ("You"), but when an argument is passed, the constructor uses its value.
The example below creates two object instances of the Test class, one without argument, and the other with an argument.
  - Example:
<?php
// object instance without argument
$obj1 = new Test();

// object instance with argument
$obj2 = new Test('Marius');
?>
- This example also demonstrate that you can create multiple object instances of the same class.
Output:
Hy You
Hy Marius

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag adds an image in web page?
<div> <img> <span>
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
}
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
});
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
}
PHP OOP - Constructor Method

Last accessed pages

  1. Create ZIP file archive with PHP (2103)
  2. Prayer The Art of Believing (1620)
  3. Get Relative Path to Website Root for Includes to Access from Anywhere (946)
  4. Multiple upload files (1172)
  5. Output or Force Download MP3 with PHP (5667)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (320)
  2. Read Excel file data in PHP - PhpExcelReader (117)
  3. The Four Agreements (96)
  4. PHP Unzipper - Extract Zip, Rar Archives (93)
  5. The Mastery of Love (85)
Chat
Chat or leave a message for the other users
Full screenInchide