PHP类中函数与变量作用域的有关问题

WBOY
Release: 2016-06-13 10:37:32
Original
689 people have browsed it

PHP类中函数与变量作用域的问题
//代码1

class Cart
{
 var $owner;
 function set_owner($name)
 {
  $this->owner=$name;
 }
}
?>

//代码2

$acart=new Cart;
$acart->set_owner("john");
print $acart->owner;
?>


代码1中函数set_owner()中的$this->owner一旦被代码2赋值,代码1中类的变量$owner是同时也被赋值了吗?如果是,函数中的变量范围不是仅限于函数中吗?如果不是,代码2中的print $acart->owner又意味着什么?



------解决方案--------------------
你先google哈 类、类实例化、继承等等概念吧
------解决方案--------------------
set_owner 方法中的 $this->owner = $name; 就是给类属性 owner 赋值的
不错,函数中的变量范围是仅限于函数中。但在这里不是冠以作用域 $this 了吗

正因为如此,同样是 function ,在类中被译做方法
------解决方案--------------------
可以这么理解

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!