English 中文(简体)
采用PHP5.3方法得出的预期成果
原标题:Strange result using instance methods in PHP 5.3

Wonder惜以下例子实际上是如何运作的,以及人们如何能够积极从事类似工作。 使用<条码> 用户_func或<条码> 用户_func_array 这样做是没有的。

<?php
class Person
{
    public $name = "George";

    public function say_hi()
    {
            return ExtraMethods::hi();
    }
}

class ExtraMethods
{
    public function hi()
    {
            return "Hi, ".$this->name;
    }
}

$george = new Person();
echo $george->say_hi();
?>

这应当导致:

Hi, George

说明为什么采用实例方法<代码>hi 不仅可以静态地称之为(并不令人惊讶的是,这种情形可能出现在购买力平价中),而且我为什么能够使用<代码>$<>。

最佳回答

From the manual:

当在物体内使用某种方法时,可动用现成的美元。 这笔钱是指电标(通常是该方法所属的物体,但也可能是另一个物体,如果该方法是从次要物体中静态地称作。)

So, according to the second portion, by design. Keep in mind it uses the actual object instance in existance though (in other words, if you add public $name = "SomethingElse"; to ExtraMethods, the result would still be Hi, George).

静态地使用这种方法并不是适当的编码,而是用于接收你的PHP,并且只发行一个小的电子版本:

"Strict Standards: Non-static method ExtraMethods::hi() should not be called statically, assuming $this from incompatible context in ..."

当然,在这种情形下,仅仅将目标当作一个论点,将更为明确和可取。

问题回答

暂无回答




相关问题
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 ...

热门标签