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 is used to add definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
PHP OOP - Constructor Method

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (137563)
  2. Rotate HTML objects, Div, Span, images with jQuery (7991)
  3. Animating in Flash - Frame-by-Frame Animation (2772)
  4. Alert, Confirm and Prompt (4521)
  5. jsSHA - SHA Hashes and HMAC in JavaScript (3427)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (252)
  2. Read Excel file data in PHP - PhpExcelReader (91)
  3. PHP Unzipper - Extract Zip, Rar Archives (76)
  4. The Four Agreements (75)
  5. The Mastery of Love (66)