PHP: Why am I getting errors about static properties?

http://codepad.viper-7.com/I0Zqoi

I don’t understand what happened to this or how to fix it or why. Can someone who knows a little about programming explain what happens behind the scenes, for example, at the translator level? Also, how can I fix my problem and why should I write the code in order to fix it? Can you tell me, in human language, what's wrong with this and how to make it better? I think my book does not explain very well, and the code inside it does not work.: / Thank you.

class A 
{
  private $foo = 'bar';
  function read()
  {
      echo self::$foo;
  }
}

$a = new A();
a::read();

Strict Standards: Non-static method A::read() should not be called statically in /code/I0Zqoi on line 13

Fatal error: Access to undeclared static property: A::$foo in /code/I0Zqoi on line 8

"" . -, (, A { read() {echo "whatever" };} :: read(), → ). , , (, A {static $variable; function read() {echo $this- > variable};} a- > read(); 't , → , .). , , . , , , . , , , . , ? , , , , , , .: (((

+5
3

Strict Standards , a::read() ::. $a A read() ->:

// Proper non-static method call
$a = new A();
$a->read();

$foo , static. , :: ->. L

// Proper reference to non-static $foo
function read() {
  echo $this->foo;
}

static? , . A::$foo :

private static $foo;

$foo A. $foo A, $foo (, new A();)

, , , .

// Static method call:
A::read();

static, static:

// Property
private static $foo;

// Method
public static function foo() {}

EDIT :

class A
{
  // Private property (non-static)
  private $foo;

  // Public property (static)
  public static $bar = 12345;

  // Public (non-static) function to access private $foo
  public function getFoo() { return $this->foo; }

  // Public (non-static) function to set private $foo
  public function setFoo($newfoo) { $this->foo = $newfoo; }

  // Static function 
  public static function incrementBar() { self::$bar++; }
}

:

// We haven't created any class instances yet (with the 'new' operator)
// But we can access the static properties & functions:

echo A::$bar . " ";
// prints 12345

A::incrementBar();
echo A::$bar . "\n";
// prints 12346

// Now we'll start working with class instances.
// Create 2 instances of class A
$a = new A();
$a->setFoo(8888);
$b = new A();
$b->setFoo(9999);

// It a violation of strict standards to call getFoo() statically
// And it meaningless to do so, because $foo only exists inside a class instance!

// Can't do this... Issues a strict warning since we're calling non-static getFoo() statically
//echo A::getFoo();


// But we can call getFoo() on the class instances:
echo $a->getFoo() . " " . $b->getFoo() . "\n";
// Prints 8888 9999

// Remember $bar was 12346...
echo $a::$bar . " " . $b::$bar . "\n";
// Prints 12346 12346

// Now modify the static property $bar again
// This affects all class instances.
A::incrementBar();
echo $a::$bar . " " . $b::$bar . "\n";
// Prints 12347 12347

-: http://codepad.viper-7.com/tV6omK

, , . /- , , . $this->property, . .

PHP error_reporting E_ALL . E_ALL & E_STRICT.

+12

:: . , ->.

$a->read();

...

echo $this->$foo;
+3

, :

, , , . , ? , , , , , , .: (((

, . . , , , . , . , , .

As to why the "debugger throws errors" (this interpreter throws E_STRICT warnings, but hey;)): this behavior has been changed in PHP by five points. In PHP 4, you can invoke any method statically, even if it was a dynamic method. Perhaps your book works on the facts.

+2
source

All Articles