-2

I was wondering is it bad practice (I'm assuming it is) to do something like this, in PHP? Common sense tells me this could lead to UB, but I haven't been able to find a reference to this in the manual, or elsewhere.

<?php
    class A
    {
        public function __construct(){}
    }
    class B extends A
    {
        public function __construct()
        {
            global $d;
            $d = new C; // How wrong is it to replace the value of $d from inside B, like this?
            exit();
        }
    }
    class C extends A
    {
        public function __construct()
        {
            echo 'C';
        }
    }

    $d = new B;

Output: C

I'd like to know the risks (if any) that come with executing code like this, and would especially appreciate a link to the manual that talks about doing something like this.

Or (since this simple code runs without errors), is this code valid, but just considered a bad practice?

user7003859
  • 673
  • 6
  • 14
  • Global variales in PHP are [considered bad practice](https://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why) - [Stop using global in PHP](https://stackoverflow.com/questions/12445972/stop-using-global-in-php) - [Why is it considered bad practice to use global reference inside functions?](https://stackoverflow.com/questions/8715897/why-is-it-considered-bad-practice-to-use-global-reference-inside-functions) - [PHP global in functions](https://stackoverflow.com/questions/5166087/php-global-in-functions) – GrumpyCrouton Nov 02 '17 at 17:53
  • Class & objects aren't made for such purposes. If you create more than 1 in different variables, it won't work... you can use it for your own code, but you shouldn't. Made some `if`'s can help –  Nov 02 '17 at 17:54
  • @GrumpyCrouton I meant to direct my question more towards the act of replacing an object from inside itself (as the title might suggest), rather than just the fact that it uses `global`. Sorry if I was unclear. – user7003859 Nov 02 '17 at 18:03
  • @user6003859 I know, I wasn't answering your question really (Otherwise I would have posted an answer), I was commenting on the use of globals. I assumed this was relevant to you as you were concerned with bad coding practices :) Doesn't make sense to use some bad coding practices if your worried about any at all. – GrumpyCrouton Nov 02 '17 at 18:04
  • Ok, then. Cheers. – user7003859 Nov 02 '17 at 18:04
  • Any reason for the downvote? – user7003859 Nov 03 '17 at 08:11

2 Answers2

0

Yes.

This is wrong at multiple levels.

  • you should not use a global state anywhere in your classes (that pertains both to global variables and singletons/registries).
  • you should not echo in from within a class's method and especially not from within a constructor
  • you should not call exit unless in context of "poor man's debugging".
  • you should not create new instance of a different class inside a constructor, because it becomes extremely hard to test and debug
  • you should not abuse the extends keyword, because it is meant for creating a specialized subtypes of the base class
tereško
  • 56,151
  • 24
  • 92
  • 147
-2

If you are expecting that variable $d will contain an instance of class C, you would be wrong. To find out, remove the exit() and dump $d at the end of your code with print_r() or var_dump().

// your code here without the exit()
echo "\n";
print_r($d);

Here is the entire output:

C
B Object
(
)

Is this a suprise? Actually, it shouldn't be.

Modifying $d inside the constructor of class B is pointless because when the constructor returns, whatever value in $d will be overwritten by a new instance of class B.

There is nothing about this is undefined behavior. Just pointless because it achieves nothing.

Rei
  • 5,938
  • 12
  • 27