Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php-可以为方法定义属性吗?_Php_Oop - Fatal编程技术网

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"

?>