English 中文(简体)
无法在公共卫生和社会福利部获得法定申报行为
原标题:Unable to get Static declaration behaviour in PHP
  • 时间:2012-01-13 17:10:23
  •  标签:
  • php
  • static
    class StaticTester
   {
      private static $id=0;
      function__construct()
     {
          self::$id+=1;
     }
    public static function checkIdFromStaticMethod()
   {
      echo "Current Id from Static method is ".self::$id;
    }


 }

  $st1=new StaticTester();
 StaticTester::checkIdFromStaticMethod();       // this outputs 1.

Okay ,I am not getting why the output is 1? After all Static means the value cannot be changed !

最佳回答

http://www.php.net/manual/en/English.oop5.static.php rel=“nofollow”的静态手段,没有“,你有可能寻找constants

问题回答
function__construct()
{
    self::id+=1;
}

应当

function__construct()
{
    self::$id+=1;
}

a. 错失美元标识:

oops.... misread the question. i 认为你在代码上错了。 你这样做了,但可能只是一份副本/帕斯特错误。

它变成一个新物体,因为每当新物体出现时,它就增加了一个。 并且所有物体均具有相同的差价。 这就是静态的含义。

a 不可更改的编号为constant,并以关键词(con在php上公布。

<代码>static并不意味着根本不能改变价值! 页: 1 <代码>static将实际保留方法电话之间的价值(因为它是成员,因此会有任何变动)。

静态手段:在一切可能情况下,将使用相同的变量

   function__construct()
     {
          self::$id+=1;
     }

 $st1=new StaticTester();

when doing the new , __construct is called , so your $id static variable will be used & increased. may you do $st2=new StaticTester() , StaticTester::checkIdFromStaticMethod() will return 2 !!! That s what your code is meant to do as it is written.

同意“随时”的答复。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...