PHP构造函数的参数 [英] PHP constructor with parameter

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

问题描述

我需要这样的函数:

  $ arr = array //这是存储数据的数组

$ f = new MyRecord(); //我在类Field()中有__constructor设置一些默认值
$ f-> {'fid'} = 1;
$ f-> {'fvalue-string'} = $ _POST ['data'];
$ arr [] = $ f;

$ f = new Field();
$ f-> {'fid'} = 2;
$ f-> {'fvalue-int'} = $ _POST ['data2'];
$ arr [] = $ f;

当我写这样的东西时:

  $ f = new Field(1,'fvalue-string',$ _POST ['data-string'],$ arr); 
$ f = new Field(2,'fvalue-int',$ _POST ['data-integer'],$ arr);

//我想使用的参数的描述:
// 1 - 总是整数,唯一(MyRecord类的fid属性)
//'fvalue-int' MyRecord类中的字段/属性的名称,其中下一个参数将去
// 3.上一个参数中指定的字段的数据
// 4.数组where class go



我不知道如何在PHP中创建参数化构造函数。



像这样:

  class MyRecord 
{
function __construct(){
$ default = new stdClass();
$ default-> {'fvalue-string'} ='';
$ default-> {'fvalue-int'} = 0;
$ default-> {'fvalue-float'} = 0;
$ default-> {'fvalue-image'} ='';
$ default-> {'fvalue-datetime'} = 0;
$ default-> {'fvalue-boolean'} = false;

$ this = $ default;
}
}


解决方案

所有这一切 http://www.php.net/manual/en/language.oop5.decon .php



构造函数可以接受参数,如php中的任何其他函数或方法

  class MyClass {

public $ param;

public function __construct($ param){
$ this-> param = $ param;
}
}

$ myClass = new MyClass('foobar');
echo $ myClass-> param; // foobar

现在你使用构造函数的例子甚至不会编译,重新分配 $ this

$



此外,每次访问或设置属性时,不需要大括号。 $ object-> property 工作正常。你只需要在特殊情况下使用大括号,如果你需要评估一个方法 $ object-> {$ foo-> bar()} ='test';


I need function that will do something like this:

$arr = array(); // this is array where im storing data

$f = new MyRecord(); // I have __constructor in class Field() that sets some default values
$f->{'fid'} = 1;
$f->{'fvalue-string'} = $_POST['data'];
$arr[] = $f;

$f = new Field();
$f->{'fid'} = 2;
$f->{'fvalue-int'} = $_POST['data2'];
$arr[] = $f;

When i write something like this:

$f = new Field(1, 'fvalue-string', $_POST['data-string'], $arr);
$f = new Field(2, 'fvalue-int', $_POST['data-integer'], $arr);

// description of parameters that i want to use: 
// 1 - always integer, unique (fid property of MyRecord class)
// 'fvalue-int' - name of field/property in MyRecord class where next parameter will go
// 3. Data for field specified in previous parameter
// 4. Array where should class go

I dont know how to make parametrized constructor in PHP.

Now i use constructor like this:

class MyRecord
{
    function __construct() {
        $default = new stdClass();
        $default->{'fvalue-string'} = '';
        $default->{'fvalue-int'} = 0;
        $default->{'fvalue-float'} = 0;
        $default->{'fvalue-image'} = ' ';
        $default->{'fvalue-datetime'} = 0;
        $default->{'fvalue-boolean'} = false;

        $this = $default;
    }
}

解决方案

Read all of this http://www.php.net/manual/en/language.oop5.decon.php

Constructors can take parameters like any other function or method in php

class MyClass {

  public $param;

  public function __construct($param) {
    $this->param = $param;
  }
}

$myClass = new MyClass('foobar');
echo $myClass->param; // foobar

Your example of how you use constructors now won't even compile as you can't reassign $this.

Also, you don't need the curly brackets every time you access or set a property. $object->property works just fine. You only need to use curly-brackets under special circumstances like if you need to evaluate a method $object->{$foo->bar()} = 'test';

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

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