Can we do this in other OO programming languages?

Can we do this in other OO programming languages? … I misunderstood about static method?

<?php
    class A {
        public static function func1() {
            return "ABC";
        }
    }

    $a = new A;
    print $a->func1() . "\n";
?>

4 thoughts on “Can we do this in other OO programming languages?

  1. Technically, you can do it. Conceptually also yes. A “class” is just another another “object” so it can have methods. But why you want to do it, i.e., why should “func1()” be a class method is a design issue.

    “static method” is merely a mean to accomplish implementing “class method” (method that the class itself is a receiver) for some languages.

  2. Looking at it again, it seems I misunderstood the original question a big bit, sorry about that.

    So now, the answer becomes: Most language will allow you to access class methods through “class of instance”. Like, self.class.classMethodName something like this.

    Some languages allow you to access them directly through instance though. Like C++ for example🙂

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s