开发者

Would a method in a static class and a static class method have the same callabilty?

开发者 https://www.devze.com 2023-03-03 05:33 出处:网络
I guess my question is would static class example1{ function example1_function(){}; } and class example2{ static function example2_function(){};

I guess my question is would

static class example1{
    function example1_function(){};     
}

and

class example2{
    static function example2_function(){};
}

lead to t开发者_如何学Che same result, which is both example1->example1_function() and example2->example2_function() have the same callabilty. Would both be defined as static and usable as such?


PHP does not allow you to declare a class static.

To call a static method, you must use the :: operator.


You cannot declare a class as a static class, as stated by other members here, but there is a method where you can stop the class from becoming an object, you can use the abstract keyword to specify the object should not be instantiated using the new keyword, this is good for inheritance etc.

abstract class Something
{
}

Doing new Something would trigger an error stating you cannot instantiate the class, you can then declare your static methods like so:

abstract class Something
{
    public static function Else()
    {
    }
}

You still have to declare you methods as static, this is just the way it is.

and then you can use like so:

Something::Else();

Hope this clears up a few thing's


As has already been mentioned in the comments, the static keyword is not used for classes in that way (syntax).

http://php.net/manual/en/language.oop5.static.php

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号