บันทึกของวีร์ | Vee(r)'s Blog

พฤศจิกายน 26, 2011

Can we do this in other OO programming languages?

Filed under: Deep geeky, PHP — वीर @ 11:31 pm

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 ความเห็น »

  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.

    ความเห็น โดย rawitat pulam — พฤศจิกายน 26, 2011 @ 11:37 pm

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

    ความเห็น โดย rawitat pulam — พฤศจิกายน 27, 2011 @ 12:19 am


RSS feed สำหรับความเห็นต่อเรื่องนี้ TrackBack URI

ใส่ความเห็น

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 / เปลี่ยนแปลง )

Connecting to %s

Theme: Shocking Blue Green. บลอกที่ WordPress.com .

Follow

Get every new post delivered to your Inbox.