Olayinka
Olayinka

Reputation: 2845

Why do these lines of code execute successfully

I'm very familiar with OOP in C++ and Java and I'm new to it in PHP, afterall PHP claims to support OOP. I'm refactoring some codes given to me in my internship and I came across the following style of coding, in PHP.

<?php
    class A {
        public function echoVar(){
            $this->__();
            echo $this->var;
        }
    }

    class B extends A{

        function __(){
            $this->var = 1;
        }
    }

    $v = new B();
    $v->echoVar();

Following my experience with C++ and especially Java, this wouldn't even try to compile, but the execution is fine in PHP. I understand that all languages aren't the same but why does PHP support this style. Is this style good or bad practices? how is this OOP?

With my knowledge, the following are what I think is wrong with this code.

I'm a OOP newbie in PHP, so please help me clarify these things.

Upvotes: 0

Views: 60

Answers (2)

Kai
Kai

Reputation: 3823

I would describe PHP as a language that tends to make assumptions when things are unclear, whereas C++ and Java are languages that force you to explicitly write everything out, or else they will throw errors.

Question #1 - In PHP you do not explicitly need to declare class properties in order to be able to assign them later, as you have discovered. Obviously though, if you immediately tried to echo $this->var; inside a class without assigning it, it would be undefined.

Question #2 - So this one's a little weird, but basically, if you create a superclass from a subclass, and you call a method inside a method defined in the subclass, the superclass version will get called. So even though __() is not defined in A, it is in defined in B, so if you create an A object, and called echoVar(); you will get a message about the method __() is undefined, you can still call echoVar(); in an object of type B, as __(); is defined there.

Question #3 - Similar to #2, even when used in the subclass, the superclass version of variables will be used.

I personally would try to never use this style of coding, however. As I'm sure you're already aware, it's not a good idea to make a class dependent on its superclass like that. It would be better to explicitly declare these things.

As for how it is OOP, the code you posted is primarily using objects as opposed to free floating functions or lines of code.

Upvotes: 1

lxg
lxg

Reputation: 13117

This is an example of, uhm, not so great coding. First, I would suggest activating strict error reporting (E_ALL | E_STRICT | E_NOTICE).

  1. If an class/object property which hasn't been declared before, is assigned a value, it is going to be created as public property of the object. However, with strict error reporting, you'd get thrown an E_NOTICE.

  2. Although the __ belongs to the B class, it can be called from A, when B is instantiated. The method is not declared as public/protected/private, so it's public by default, therefore accessible by parents/children as well as from the outside.

  3. Of course, this is generally poor coding style, at least A would demand the implementation of __ in child classes with the following line: abstract public function __();.

  4. Last, but not least, the function name __ is against PHP conventions, as methods starting with two underscores are reserved.

Upvotes: 1

Related Questions