如何将数组传递给php中的类?

时间:2017-11-27 22:06:00

标签: php class

SMTP.module is active.
The configuration options have been saved.
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "220 email-smtp.amazonaws.com (hide key) "
SMTP -> get_lines(): $data was "220 email-smtp.amazonaws.com ESMTP (hide key) "
SMTP -> FROM SERVER: 220 email-smtp.amazonaws.com ESMTP (hide key)
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "250-email-smtp.amazonaws.com "
SMTP -> get_lines(): $data was "250-email-smtp.amazonaws.com "
SMTP -> get_lines(): $data was "250-email-smtp.amazonaws.com "
SMTP -> get_lines(): $str is "250-8BITMIME "
SMTP -> get_lines(): $data was "250-email-smtp.amazonaws.com 250-8BITMIME "
SMTP -> get_lines(): $data was "250-email-smtp.amazonaws.com 250-8BITMIME "
SMTP -> get_lines(): $str is "250-SIZE 10485760 "
SMTP -> get_lines(): $data was "250-email-smtp.amazonaws.com 250-8BITMIME 250-SIZE 10485760 "
SMTP -> get_lines(): $data was "250-email-smtp.amazonaws.com 250-8BITMIME 250-SIZE 10485760 "
SMTP -> get_lines(): $str is "250-AUTH PLAIN LOGIN "
SMTP -> get_lines(): $data was "250-email-smtp.amazonaws.com 250-8BITMIME 250-SIZE 10485760 250-AUTH PLAIN LOGIN "
SMTP -> get_lines(): $data was "250-email-smtp.amazonaws.com 250-8BITMIME 250-SIZE 10485760 250-AUTH PLAIN LOGIN "
SMTP -> get_lines(): $str is "250 Ok "
SMTP -> get_lines(): $data was "250-email-smtp.amazonaws.com 250-8BITMIME 250-SIZE 10485760 250-AUTH PLAIN LOGIN 250 Ok "
SMTP -> FROM SERVER: 250-email-smtp.amazonaws.com 250-8BITMIME 250-SIZE 10485760 250-AUTH PLAIN LOGIN 250 Ok
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "334 VXNlcm5hbWU6 "
SMTP -> get_lines(): $data was "334 VXNlcm5hbWU6 "
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "334 UGFzc3dvcmQ6 "
SMTP -> get_lines(): $data was "334 UGFzc3dvcmQ6 "
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "235 Authentication successful. "
SMTP -> get_lines(): $data was "235 Authentication successful. "
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "250 Ok "
SMTP -> get_lines(): $data was "250 Ok "
SMTP -> FROM SERVER: 250 Ok
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "250 Ok "
SMTP -> get_lines(): $data was "250 Ok "
SMTP -> FROM SERVER: 250 Ok
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "354 End data with <CR><LF>.<CR><LF> "
SMTP -> get_lines(): $data was "354 End data with <CR><LF>.<CR><LF> "
SMTP -> FROM SERVER: 354 End data with <CR><LF>.<CR><LF>
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "250 Ok 0101015fff5750b0-3f553350-bcf3-49e5-affd-b867b1a37e5f-000000 "
SMTP -> get_lines(): $data was "250 Ok 0101015fff5750b0-3f553350-bcf3-49e5-affd-b867b1a37e5f-000000 "
SMTP -> FROM SERVER: 250 Ok 0101015fff5750b0-3f553350-bcf3-49e5-affd-b867b1a37e5f-000000
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "221 Bye "
SMTP -> get_lines(): $data was "221 Bye "
SMTP -> FROM SERVER: @byemsg
A test e-mail has been sent to x@gmail.com You may want to check the logs for any error messages.
SMTP.module is active.

因此,当我调用此函数ispis()时,它不做任何事情,但当我在函数__constructor内部回显时,它会显示输入的所有内容的正确值。此外,当我评论上面的前三行// $ daniel = new User(&#34; ivan&#34;,&#34; 22&#34;);行和取消注释,ispis()工作正常。如果有人能向我解释为什么会这样,那会很好。 Tnx提前:))

3 个答案:

答案 0 :(得分:2)

根据代码的外观,您尝试将两个新用户实例传递给新用户(“daniel”)。

所以基本上用户期待4个参数(年龄,名字,身高,体重)。你已经正确地创建了Luka和Ivan,但是在尝试创建Daniel时,你将这两个用户作为参数传递。当你想要年龄,名字,身高和体重时,你会给它Luka和Ivan。

如果您只想将数组传递给构造函数,只需将其作为参数传递给新实例:

<?php
class User {

    public $name;
    public $age;
    public $height;
    public $weight;

    function __construct($args){

        $this->age = $args['age'];
        $this->name = $args['name'];
        $this->height = $args['height'];
        $this->weight = $args['weight'];

    }

    function getAge() {
        return $this->age;
    }
}

$question_array = [
    'name' => 'Daniel',
    'age' => '22',
    'weight' => '174',
    'height' => '68'
];

$daniel = new User($question_array);
echo $daniel->getAge(); // 22

?>

答案 1 :(得分:0)

Robert刚回答,他的方法是我在个人MVC中用来定义构造的方法,或者在另一种方式中我也总是在我的MVC中使用这种方法:P ... 顺便说一下,我认为伊万你对课程感到困惑......

class User{

public $name ;
public $age;
public $height ;
public $weight ;

 function __construct($array){

    if (is_array($array)){
        foreach($array as $k=>$v){
            $this->$k = $v;
        }
     }

  }
  public function ispis(){

      print 'NAME :' .$this->name .' AGE : ' .$this->age.' HEIGHT : ' .$this->height .' WEIGHT : ' .$this->weight.'<br>';

  }

}

 $arrayOne = ['name'=>"Ivan",'age'=>"22",'height'=>"174",'weight'=>"68"];
 $arrayTwo = ['name'=>"Luke",'age'=>"23",'height'=>"174",'weight'=>"68"];

  $ivan = new User($arrayOne);
  $luka = new User($arrayTwo);
  $ivan->ispis();
  $luka->ispis();

答案 2 :(得分:0)

你的问题看起来有点模棱两可,但也许你想从一组参数中创建一个对象。

<?php
class User {

    public $name;
    public $age;

    public function __construct($name, $age)
    {
        $this->name = $name;
        $this->age  = $age;
    }

    public function echoAge()
    {
        echo $this->age;
    }
}

$args  = ['Leonard', 21];
$bones = new User(...$args);
$bones->echoAge();

输出:

21