Class instantiation causing functions within said class to be executed PHP - php

I have a registration class. The problem I'm facing is that the instantiation itself is somehow causing the functions within that class to be called.
I've tested this by adding an error_log() directly before and after the instantiation: $register = new Register(); every time I receive another error_log() which I placed inside the functions of the class that I'm instantiating.
How can I solve this?
EDIT this is what an example may look like:
testclass.php
class Test {
public function test() {
error_log("Function test() was run.'");
}
}
test.php
require_once("testclass.php");
$test = new Test();

It's because your function, test() has the same name of the class Test so PHP is using it as the constructor and calling it when you instantiate it.
class Bar {
public function Bar() {
// treated as constructor in PHP 5.3.0-5.3.2
// treated as regular method as of PHP 5.3.3
}
}
As the docs say:
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

Why is a PHP class method called on instantiation?

Need some help understanding what's going on in my PHP code.
index.php
include("cache.php");
$cache = new Cache();
cache.php
class Cache {
private static $URL_CACHE;
public function cache($url, $entityId) {
echo '<br>caching '.$url.' as '.$entityId;
}
When I request index.php, I get 'caching as' displayed, which is a surprise. I never called $cache->cache('', '');
What's calling the method?
As per Blake's answer, since the method name matches (somewhat) the class name, it's called on instantiation. For Java developers this will certainly be a surprise.
This is using deprecated PHP functionality to act as a __contruct() method. In older versions of PHP (Removed in 7) If you had a class named Foo and a function named foo() it was how you would call it as a constructor.
In short, this is being called by you instantiating the class. If you change your cache() method to makeCache() I bet it will go away.
Another solution is to have an empty constructor as well, thanks JimL.
public function __construct() {
}

Constructor is not getting called

I have a strange problem with one of my classes.
The class is the next one
namespace Core;
class RequestHandler{
protected $app;
public function RequestHandler($app){
echo "EEE";
$this->app = $app;
}
}
And the initialization is
$requestHandler = new Core\RequestHandler($app);
I don't know why it doesn't show anything, but if I change the constructor to __construct everything works fine.
I'm using php 5.6.20 and I know that it should execute the constructor by name too.
Check example here:
<?php
namespace Foo;
class Bar {
public function Bar() {
// treated as constructor in PHP 5.3.0-5.3.2
// treated as regular method as of PHP 5.3.3
}
}
?>
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.
So in 5.6.20 constructor by name is not deprecated.
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.
You may try to remove namespace, but I don't think it's good way to solve it.

Running code in a PHP once

I'm trying to write a class in PHP that acts as a wrapper for a collection of command line tools to make them easier to use from PHP.
I have a single class (MyClass) in a file myclass.php.
I have code that checks to see if the required tools are installed and then sets a constant (TOOLS_AVAILABLE) to either true or false. Although it's not a lot of code, I only want it to run the first time somebody tries to instantiate my class or use any of its static functions. What's the best practice for handling this?
I only want it to run the first time somebody tries to instantiate my class or use any of its static functions.
Well, the best answer is not to have any static methods. Then you can stick the code in a constructor method as per the answer by #treegarden.
If you must have static methods, then you'll need a static flag within the class to indicate when you've called the 'run once' code, so you can avoid running it again. And then call it explicitly from each of your static methods and the constructor. Something like this:
<?php
class myClass {
private static $hasRunOnce = false;
private static runMeOnce()
{
if (!self::$hasRunOnce) {
self::$hasRunOnce = true;
//put your 'run once' code here...
}
}
public static oneOfYourStaticMethods()
{
self::runMeOnce();
//put your static method code here...
//this would be the same for each of your static methods and your constructor.
}
}
Hope that helps.
You need to create a __construct function in your class and put whatever code you want to execute on instantiation in there:
class MyClass {
function __construct(/* arguments */) {
/* your code here */
}
}
The code will get executed only once when someone instantiates the class.

How to handle a call to an undefined function in a PHP unit test

I am relatively new to unit testing, so maybe someone can help me out here.
Problem
The following error message appears when executing the PHP unit test in the terminal:
Fatal error: Call to undefined function Path\to\missing_function() in /path/to/file.php on line 123
Normally, I would now create a dummy object using the getMock(originalClassName) function and then predefine what should be returned for missing_function() but sadly the function is not placed in any interface/class like all other functions I tested up to now.
Anyone got an idea here? Cheerio!
Generally speaking, you would just include the file with the function and have it get executed. Then you just make sure that after your code was executed, what was supposed to happen happens. You are more concerned with the outcome of the code that you are testing rather than the process of how your code is executed.
Though, if you need to mock a function there is a way using namespaces (need PHP 5.3+). In your test file, you can place a "mock" function that is in the same namespace as your code. When the function gets called php looks in the current namespace first and will find your replacement function. In the normal running of your code, it will proceed to the global namespace to call your function.
So you code would end up like this:
Your class:
namespace Foo;
class SUT {
public function bar() {
return baz();
}
}
Your test:
namespace Foo;
function bar() {
return 'boz';
}
class SUTTest extends PHPUnit_Framework_TestCase {
public function testBar() {
$sut = new SUT();
$this->assertEquals('boz', $sut->bar());
}
}

What is the difference between the two types of class constructors in PHP?

What exactly is the difference in PHP classes when using the __construct constructor and when using the name of the class as constructor?
For example:
class Some
{
public function __construct($id)
{
....
}
....
}
OR
class Some
{
public function Some($id)
{
....
}
....
}
The top is the new way it is done in PHP as of version 5.0 and is how all new code should be written. The latter is the old PHP 4 way and is obsolete. At some point it will be completely deprecated and removed from PHP altogether.
Update
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.
<?php
namespace Foo;
class Bar {
public function Bar() {
// treated as constructor in PHP 5.3.0-5.3.2
// treated as regular method as of PHP 5.3.3
}
}
?>

Categories