I'm a little confused about the situation shown in this code...

class DirEnt
{
    public function PopulateDirectory($path)
    {
        /*... code ...*/

        while ($file = readdir($folder))
        {
            is_dir($file) ? $dtype = DType::Folder : $dtype = Dtype::File;                       
            $this->push_back(new SomeClass($file, $dtype));
        }

        /*... code ...*/
    }

    //Element inserter.
    public function push_back($element)
    {
        //Insert the element.
    }
}

Why do I need to use either $this->push_back(new SomeClass($file, $dtype)) or self::push_back(new SomeClass($file, $dtype)) to call the member function push_back? I can't seem to access it just by doing push_back(new SomeClass($file, $dtype)) like I would have expected. I read When to use self over $this? but it didn't answer why I need one of them at all (or if I do at all, maybe I messed something else up).

Why is this specification required when the members are both non-static and in the same class? Shouldn't all member functions be visible and known from other member functions in the same class?

PS: It works fine with $this-> and self:: but says the functions unknown when neither is present on the push_back call.

Answer
    • That's just how PHP works. In this case, you need $this->push_back(). If you want to know specifically, you can download and read the PHP source code. I think it's odd that you don't have to in some languages, because that's not what I'm used to.
      • 2
    • And thanks for the good comment, it makes sense. I just haven't seen that in languages before, so I figured I was messing up the syntax.
      • 1
    • PHP could support it, but I think it would be problematic and cause more confusion than be useful (for instance, PHP has a lot of global functions, and is not strictly OOP). Also note, I didn't downvote the question.
    • Ah I didn't think it was you :) I've asked stupid questions plenty, I just like to know why people downvote so I know they're studpid, haha... and yeah, PHP is a little different with all the globals - it's pretty nice to learn though. It's quick like python or perl :)

$this->push_back will call the method as part of the CURRENT object.

self::push_back calls the method as a static, which means you can't use $this within push_back.

push_back() by itself will attempt to call a push-back function from the global scope, not the push_back in your object. It is not an "object call", it's just a plain-jane function call, just as calling printf or is_readable() within an object calls the usual core PHP functions.

  • 8
Reply Report

I cant seem to access it just by doing push_back(new SomeClass($file, $dtype)) like I would have expected.

This way you call push_back() as a function. There is no way around $this (for object methods) or self::/static:: (for class methods), because it would result into ambiguity

Just remember: PHP is not Java ;)

  • 7
Reply Report
    • In all fairness, as other languages do support this type of thing, it may seem strange that PHP doesn't (checking for local methods). But PHP has such a syntax that I think it would be more problematic than helpful to allow class-internal method calls without a scope resolution. Oh well.
      • 1
    • AFAIK it's also possible to call $this::static_method() when you are in the instance object context. And you can also use static::static_method() if you need late static binding (call the method on the actual type of object).

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions