PHP类参数的函数 [英] PHP Class arguments in functions

查看:100
本文介绍了PHP类参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是从我的类的参数在该类的功能之一有问题。

I'm having trouble using an argument from my class in one of that class's functions.

我有一个类叫做公司:

class company {

   var $name;

   function __construct($name) {
      echo $name;
   }

   function name() {
      echo $name;
   }
}

$comp = new company('TheNameOfSomething');
$comp->name();

当我将它实例(倒数第二行),构建魔术方法做工精细,并回声去TheNameOfSomething。然而,当我调用name()函数,我什么也没有。

When I instantiate it (second to last line), the construct magic method works fine, and echos out "TheNameOfSomething." However, when I call the name() function, I get nothing.

我是什么做错了吗?任何帮助是极大的AP preciated。如果您需要任何其他信息,只问!

What am I doing wrong? Any help is greatly appreciated. If you need any other info, just ask!


-Giles

http://gilesvangruisen.com/

推荐答案

您需要使用$ this关键字来设置类属性。

You need to set the class property using the $this keyword.

class company {

   var $name;

   function __construct($name) {
      echo $name;
      $this->name = $name;
   }

   function name() {
      echo $this->name;
   }
}

$comp = new company('TheNameOfSomething');
$comp->name();

这篇关于PHP类参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆