Hướng dẫn dùng statically trong PHP

Tip

This page describes the use of the static keyword to define static methods and properties. static can also be used to define static variables and for late static bindings. Please refer to those pages for information on those meanings of static.

Việc khai báo các thuộc tính và phương thức của lớp là static [tĩnh] sẽ giúp ta có thể truy cập các thành phần này mà không cần tạo thể hiện của lớp tương ứng. Một thuộc tính được khai báo là static thì không thể được truy cập thông qua thể hiện của đối tượng của lớp tương ứng [phương thức static thì có thể].

Từ phiên bản PHP 4, nếu không khai báo tầm vực thì thuộc tính và phương thức của lớp sẽ mặc định có tầm vực là public.

Phương thức tĩnh

Do các phương thức được gọi mà không cần phải thông qua thể hiện của đối tượng, nên biến giả $this là không cần và không được sử dụng được trong phương thức tĩnh.

Caution

In PHP 5, calling non-static methods statically generates an E_STRICT level warning.

Warning

In PHP 7, calling non-static methods statically is deprecated, and will generate anE_DEPRECATED warning. Support for calling non-static methods statically may be removed in the future.

Ví dụ về phương thức tĩnh

Thuộc tính tĩnh

Thuộc tính tĩnh không thể được truy cập thông qua việc sử dụng toán tử mũi tên ->.

Cũng giống như bất kỳ biến tĩnh nào, các thuộc tính static chỉ có thể được khởi tạo bằng cách sử dụng một literal hoặc một hằng trước trước phiên bản PHP 5.6; các biểu thức là không được phép. Từ bản PHP 5.6 về sau thì các quy tắc tương tự được áp dụng như biểu thức hằng: một số biểu thức some limited expressions are possible, provided they can be evaluated at compile time.

As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value cannot be a keyword [e.g. self, parent and static].

Ví dụ về thuộc tính static

Lưu ý rằng bạn nên đọc phần Phạm vi của biến nếu bạn đang tìm kiếm từ khóa static sử dụng cho việc khai báo các biến tĩnh bên trong hàm hay phương thức. I myself had this gap in my PHP knowledge until recently and had to google to find this out. I think this page should have a "See also" link to static function variables.
//www.php.net/manual/en/language.variables.scope.php

Một số lưu ý sử dụng:

Để kiểm tra xem một hàm nào đó có được gọi static hay không thì ta làm như sau:

Here statically accessed property prefer property of the class for which it is called. Where as self keyword enforces use of current class only. Refer the below example:

webmaster at removethis dot weird-webdesign dot de ¶

6 years ago

On PHP 5.2.x or previous you might run into problems initializing static variables in subclasses due to the lack of late static binding:

This will output:
A::$a = lala; B::$a =

If the init[] method looks the same for [almost] all subclasses there should be no need to implement init[] in every subclass and by that producing redundant code.

Solution 1:
Turn everything into non-static. BUT: This would produce redundant data on every object of the class.

Solution 2:
Turn static $a on class A into an array, use classnames of subclasses as indeces. By doing so you also don't have to redefine $a for the subclasses and the superclass' $a can be private.

Short example on a DataRecord class without error checking:

I hope this helps some people who need to operate on PHP 5.2.x servers for some reason. Late static binding, of course, makes this workaround obsolete.

ssj dot narutovash at gmail dot com ¶

8 years ago

It's come to my attention that you cannot use a static member in an HEREDOC string.  The following code

class A
{
  public static $BLAH = "user";

  function __construct[]
  {
    echo

gratcypalma at gmail dot om ¶

5 years ago

Mirco ¶

5 years ago

The simplest static constructor.

Because php does not have a static constructor and you may want to initialize static class vars, there is one easy way, just call your own function directly after the class definition.

for example.

b1tchcakes ¶

3 months ago

Chủ Đề