Why does PHP call index method in Index class automatically? [duplicate] - php

This question already has an answer here:
Why is a method with the same name as the class being called automatically?
(1 answer)
Closed 4 years ago.
I have the following snipet:
<?php
class Index
{
public function index()
{
echo 'Hello';
}
}
new Index();
It prints out 'Hello' without explicit call of index method. Any change in the name of the class or the method mutes the echo. Why does PHP call index method in Index class automatically? Why does it handle differently when class is renamed to Index2?

This is a old PHP feature where functions with the same name as the class are treated as a constructor. As mentioned in the documentation for constructors:
For backwards compatibility with PHP 3 and 4, if PHP cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics.
and also,
Warning Old style constructors are DEPRECATED in PHP 7.0, and will be removed in a future version. You should always use __construct() in new code.

Related

Call to a member function AddAddress() on null [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
what is the function __construct used for?
is there any difference between __construct function and function with same name as class has?
class foo {
function foo ($something){
echo "I see ".$something." argument";
}
}
class bar {
function __construct ($something){
echo "<br />
I see ".$something." argument again";
}
}
$foo = new foo("foo");
$bar = new bar("bar");
The method named is the PHP4 way of doing a constructor.
For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics.
As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes.
http://www.php.net/manual/en/language.oop5.decon.php
Constructor function named same as class is a backward compatibility feature for PHP4. It will not work with namespaced classes since PHP 5.3.3
If both __construct and class-named functions are present, then the __construct is used as constructor.
The first one is old php4 style "construct". It is basically the same as the __construct.
The difference is that calling a constructor by the same name of the class is deprecated.
The difference is that PHP version 5.3.3 and above will treat function foo() as regular method and not constructor. Previous versions will treat it as a constructor.

Why is this method called without being asked to?

I came across this very strange behavior.
The following code
class TestClass {
function testClass() {
echo "Don't show me!";
}
}
$testing = new TestClass;
executes its method testClass without it being called!
However, testClass won't run if renamed into anything else like testClass1.
Is there any hidden 'PHP magic' behind this behaviour?
EDIT.
At the end I see this question is trivial to ninjas grown up with PHP. As recent newcomer to PHP, I've learned to use __construct as constructor. With that "relic behaviour" carefully removed from modern tutorials. I am so glad people realized how terrible it was, changing class name and forgetting to change that of the constructor - what a nightmare!
Pre-PHP5, the __construct method was not used as the class constructor. Instead, a method with the same name as the class was used.
From the documentation:
For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, and the class did not inherit one from a parent class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics.
Creating a (empty) constructor (method named __construct) will stop the message from being echoed upon class initialization (only needed for < PHP 5.3.3 *):
class TestClass {
function __construct() {
}
function testClass() {
echo "Don't show me!";
}
}
* As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes.
In older versions of PHP a method with the same name as the classname was considered the constructor.

$class->method() or $class::method() [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
where we use object operator “->” in php
In PHP 5, what are the advantages of typing $class::method() instead of $class->method()?
As in any performance or functional differences. Or is this just a way of forcing code non-PHP4 friendly because of the complete rewrite?
In PHP5, the two aren't interchangeable.
Static method calls will perform faster than non-static calls (over many iterations) but then the method is called in the static context and there is no object available to the called method.
The only reason PHP lets you call a non-static method using the static notation was for backwards compatibility in PHP 4 (because PHP 4 didn't have the static modifier for functions, or public/protected/private). If you do call a non-static method statically, you get a warning about "Strict Standards" output, and eventually this may fail with a fatal error.
So the answer really is to call the method the way it was supposed to be called. If it is a static method in PHP 5, then call it statically Class::method(), if it is a public method, then call it using the object $class->method().
Consider this code (run in PHP 5):
class Foo {
protected $bar = 'bar';
function f() {
echo $this->bar;
}
}
echo Foo::f(); // Fatal error: Using $this when not in object context
$class::method() calls a static method of the class whereas $class->method() calls a public standard method of the class.

Shorthand new Instance->Method syntax in PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: How to chain method on a newly created object?
I can create an instance and call it's method via:
$newObj = new ClassName();
$newObj -> someMethod();
But is there any way I can do it in a shorter notation, an anonymous instance? I tried this:
(new ClassName())->someMethod();
But it doesn't seem to work as expected.
Additional Info: The method I want to call is public but not static.
PHP 5.4 supports it.
If you can't update you can workaround like this:
function yourclass($param) {
return new yourclass($param);
}
yourclass()->method();
Don't forget that your constructor must return $this;
Not that i know of.
But! - You could implement a Singleton Pattern and then call:
ClassName::getInstance()->someMethod();
Or, to cut it short ;)
ClassName::gI()->someMethod();
If someMethod does not refer to $this, you could also simply call it as a static function, though it wasn't defined as one:
ClassName::someMethod();
If the method is static and doesn't rely on any class variables (maybe you've put it in a class just for organisation purposes), you can simply call it staticly as phil is demonstrating with getInstance:
ClassName::someMethod()

What's difference between __construct and function with same name as class has? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
what is the function __construct used for?
is there any difference between __construct function and function with same name as class has?
class foo {
function foo ($something){
echo "I see ".$something." argument";
}
}
class bar {
function __construct ($something){
echo "<br />
I see ".$something." argument again";
}
}
$foo = new foo("foo");
$bar = new bar("bar");
The method named is the PHP4 way of doing a constructor.
For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics.
As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes.
http://www.php.net/manual/en/language.oop5.decon.php
Constructor function named same as class is a backward compatibility feature for PHP4. It will not work with namespaced classes since PHP 5.3.3
If both __construct and class-named functions are present, then the __construct is used as constructor.
The first one is old php4 style "construct". It is basically the same as the __construct.
The difference is that calling a constructor by the same name of the class is deprecated.
The difference is that PHP version 5.3.3 and above will treat function foo() as regular method and not constructor. Previous versions will treat it as a constructor.

Categories