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
What attribute makes a radio button or checkbox input selected?
checked="checked" selected="selected" disabled="disabled"
<input type="checkbox" name="a_name" value="value" checked="checked" />
What CSS value scales the background image to the largest size contained within the element?
repeat-x contain linear-gradient
#id {
  background:url("path_to_image.png");
  background-size:contain;
  background-repeat:no-repeat;
}
What operator is used to determine the rest of the division of two numbers?
% * /
var rest8_7 = 8 % 7;
alert(rest8_7);
Indicate the PHP function that rounds a number up to the next highest integer.
floor() ceil() abs()
$nr = ceil(3.5);
echo $nr;        // 4
PHP OOP - Constructor Method

Last accessed pages

  1. Voting Poll System script PHP-AJAX (8743)
  2. ActionScript 3 Lessons (7369)
  3. Merge Multiple Files, Line by Line (1137)
  4. Vue JS - Short presentation (423)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142069)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (72)
  2. The Mastery of Love (17)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (10)
  4. CSS cursor property - Custom Cursors (10)
  5. PHP Unzipper - Extract Zip, Rar Archives (9)