Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP autoload and static variable in function

=== Base.php ===

<?php
class Base
{
    public static function e()
    {
        static $number = 0;
        $number++;
        var_dump($number);
    }
}

=== A.php ===

<?php
class A extends Base {}

=== B.php ===

<?php
class B extends Base {}

=== test.php ===

function __autoload($classname)
{
    require_once("{$classname}.php");
}

Base::e();
A::e();
B::e();

php test.php, result is:

int(1)
int(2)
int(2)

Why not result is 1,1,1?

like image 633
Jelly Avatar asked Sep 09 '15 08:09

Jelly


People also ask

How can use static variable in class in PHP?

The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.

Can we increment static variable in PHP?

The increment_counter() function will increment the value of the static variable by 1. The self keyword is used in the script to read and increment the value of the static variable. echo $object->increment_counter()."

How use $this in static method in PHP?

You can't use $this inside a static function, because static functions are independent of any instantiated object. Try making the function not static. Edit: By definition, static methods can be called without any instantiated object, and thus there is no meaningful use of $this inside a static method.

How can access public static variable in PHP?

Static properties are accessed using the Scope Resolution Operator ( :: ) and cannot be accessed through the object operator ( -> ). It's possible to reference the class using a variable. The variable's value cannot be a keyword (e.g. self , parent and static ). print $foo::$my_static .

Can I use a static variable inside a function in PHP?

(If the function is contained within a class, you may be better using a private or protected class variable instead of a static variable inside the function). The PHP code example below shows a function which uses a static variable.

Is there a way to autoload plain functions in PHP?

I prefer to use composer's autoloader, but this works for legacy projects that can't use composer. You don't need exceptions to figure out if a class can be autoloaded. This is much simpler. Autoloading plain functions is not supported by PHP at the time of writing. There is however a simple way to trick the autoloader to do this.

What is the difference between global and static variables in PHP?

There may be times when a static variable is needed in a PHP function; static variables maintain their value between function calls and are tidier than using a global variable because they cannot be modified outside of the function.

How do I auto load a PHP script?

Mine is set to ... The auto_loader.php script contains a single function. The __autoload () function. The include_dir path IS examined to find this file, so you can just put it with the rest of your includable files. A useful additional facility here is that you could log which classes are used by a script at runtime.


1 Answers

Try

require "Base.php";
Base::e();
require "A.php";
A::e();

vs.

require "Base.php";
require "A.php";
Base::e();
A::e();

The former will yield int(1) int(2), while the latter yields int(1) int(1).

Why?

When a class is bound, the static variable content is copied at exactly that moment how it currently is. There is no backing up of the original value of the static variable.

That implies, when the static variable is 0 when class A is bound, A::e() will have 0 as static value; in case it's 1, A::e() will also have 1 as value.

Similar for B::e() then, as Base::e() and A::e() are independent as the values are copied (no references). It will also have the same static variable Base::e() has at the binding time of B.

like image 110
bwoebi Avatar answered Oct 11 '22 03:10

bwoebi