dougaoshang0237 2015-06-11 15:50
浏览 45
已采纳

php中带有参数问题的构造函数

I'm not able to get the arguments passed onto a function in the constructor. Let me a bit clear with the code snippet below.

class Foo{
    public function __construct($arg){
         $arg = $arg1;
    }

    public function A($arg1){
        echo $arg1;
    }
}

$obj = new Foo();
$data = $obj->A('hello');

Here, the call is being made to the function A with an argument. I'm not able to get the same argument value in the constructor. I'm just giving an idea of my code.

  • 写回答

3条回答 默认 最新

  • dongmin4990 2015-06-11 15:59
    关注

    Some additional examples:

    Constructor Argument

    class Foo{
        public function __construct($argument){
            echo $argument;
        }
    }
    
    new Foo('Hello World');
    

    Constructor Argument + Set argument to object as property

    class Foo
    {
        public $argument = '';     
    
        public function __construct($argument){
            $this->argument = $argument;
        }
    
        public function printIt()
        {
            echo $this->argument;
        }
    }
    
    $foo = new Foo('Hello World');
    $foo->printIt();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序