php-可以为方法定义属性吗?
我想在方法中定义一些包含在该方法中的属性,即不是整个类的属性。但我们希望从对象的实例中执行此操作。可能吗?下面的代码不起作用php-可以为方法定义属性吗?,php,oop,Php,Oop,我想在方法中定义一些包含在该方法中的属性,即不是整个类的属性。但我们希望从对象的实例中执行此操作。可能吗?下面的代码不起作用 class foo{ var $x; function __construct(){ } function abc(){ echo $y; } } $new = new foo(); $new->abc->y = "bar"; 希望这会有帮助。这还没有奏效 /* * I have trimmed this down
class foo{
var $x;
function __construct(){
}
function abc(){
echo $y;
}
}
$new = new foo();
$new->abc->y = "bar";
希望这会有帮助。这还没有奏效
/*
* I have trimmed this down because of redundant entries, and only
* modified what you had written
*/
class foo
{
/*
* I have made this public so you can access this globally
*/
public function abc($y)
{
echo $y;
}
}
$new = new foo();
/*
* Instead of passing a value directly to the variable inside the
* method, I made the method accept properties that will modify
* the variable. You cannot do this if you have not made the method
* 'public'.
*/
$new->abc("bar"); // the method contains an echo statement, so it will echo "bar"
?>