当前位置: 动力学知识库 > 问答 > 编程问答 >

Method invocation in PHP

问题描述:

I have TestClass and a public method in it.

I am able to call the same method using :: operator like static method and using an object.

What are the advantages or use of static functions in PHP, if we able to call public functions without creating object of the same class?

<?php

class TestClass {

public function testMethod() {

echo 'Method called';

}

}

TestClass::testMethod();

$classObj = new TestClass();

$classObj->testMethod();

?>

网友答案:

In low-level terms, a static function in PHP isn't much different than a member function. The only real difference is that $this isn't provided to a static function.

That means the use of the static keyword is mostly of semantic benefit, as it helps you define the architecture and intended behaviour of your classes.

With that said, you shouldn't abuse the semantics. PHP can (optionally) warn you about those kinds of mistakes, and you should always pay attention to such warnings. The language specification is there for a reason, and it's designed to work in certain ways. If you use static (or any other language feature) incorrectly, then it may not always work as you expect. Future PHP updates or configuration changes could break your code unexpectedly.

网友答案:

In this case, there is no difference.

However, the point of static functions is to say that some functions don't need an instance of the class in order to be executed. It is possible to call functions statically even if they are not marked as static, but it is technically incorrect to do so. If you have error_reporting(E_ALL) set, it will give you a strict standards error.

This is not because the code won't work, but because it might not.

class TestClass {
    private $name = 'Rakesh';
    public function doSomething() {
        echo "Hi there";
    }
    public function doSomethingElse() {
        echo "Hi there " . $this->name;
    }
}

You can call the first function statically and it will work fine. But if you call doSomethingElse statically, it won't work, because it tries to access $this, which is only possible if you have an object.

So we apply the static keyword to doSomething to let (a) PHP and (b) the programmer using the class know that it is possible to call it statically. It's a promise that it will work.

The assumption should be that, if it is not marked as static, you shouldn't call it statically.

PHP's strict standards errors are meant to make your code better, even if it already works. The documentation for the E_STRICT constant says:

Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.

网友答案:

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

For compatibility with PHP 4, if no visibility declaration is used, then the property or method will be treated as if it was declared as public. Advantages are ...

1>Hash memory will not create ,hence no wastage of Memory (no memory leak problem)

2>

// This makes little sense
Math m = new Math();
int answer = m.sin(45);

// This would make more sense
int answer = Math.sin(45);
网友答案:

It's like a shortchut, one feature more of php. But, to access to their properties you must declare them like constants. For example:

<?php
class Math{
       const pi=3.1416; 
}
echo Math::pi;
?>
分享给朋友:
您可能感兴趣的文章:
随机阅读: