9

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.

7ochem
  • 2,183
  • 1
  • 34
  • 42
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
  • 2
    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. – Jared Farrish Oct 02 '11 at 20:15
  • 1
    Thanks for the anonymous downvote :p – John Humphreys Oct 02 '11 at 20:19
  • 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. – John Humphreys Oct 02 '11 at 20:20
  • 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. – Jared Farrish Oct 02 '11 at 20:22
  • 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 :) – John Humphreys Oct 02 '11 at 20:24
  • PHP, due to historical and practical reasons, is somewhat different. You're probably better off coming from Java, since it's much more heavy handed than the sometimes flighty PHP. But PHP is much faster to prototype and deploy than Java, as long as you're careful, and personally I love the [variable expansion feature](http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing) PHP was built on top of. – Jared Farrish Oct 02 '11 at 20:32

3 Answers3

8

$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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
7

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 ;)

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • 1
    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. – Jared Farrish Oct 02 '11 at 20:20
  • 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). – Pavel S. Oct 02 '11 at 20:36
0

You can access like this

public static function abc($process_id){
return 1;
}
public static function xyz(){
$myflag=self::abc();
return $myflag;
}
output : 1
Ajay
  • 421
  • 5
  • 6