PHP and non-existing constructors.

Written by Johannes on June 3, 2012 Categories: php

Your ads will be inserted here by

Easy AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

I just found a nice (or lets say not so nice) unexpected behavior in PHP. Apparently anything goes with constructors.

Calling a constructor that hadn’t been defined did not throw any kind of exception, error, warning, whatsoever.

So the following example executes:

class TestClass {
  public function testFunction() {
    return "Hello World!";
  }
}
 
$testClass = new TestClass ("blabla"); // should report an error
echo $testClass->testFunction(); // Hello World

Update: Apparently there is a rationale behind this: the PHP function func_get_arg() allows shell style retrieval of function arguments.

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre user="" computer="" escaped="">