警告:缺少参数1 [英] Warning: Missing Argument 1
问题描述
我的php代码有一些问题:所有信息都返回了,但是我无法弄清楚为什么会出错.对于我的索引页,我仅包含实际使用该类的代码行,除了包含的代码外,实际上没有其他代码.我确定这是我构建__contstruct的方式,但是我不确定这样做的适当方式.在索引页中如何调用它时,我丢失了一些东西.
I am having some problems with my php code: All information returns but I cannot figure out why I am getting the error. For my index page I only inluded the line of code that is actually using that class there really is no other code other than some includes. Im sure it is how I built my __contstruct but i am not sure of the approriate way of doing it. I am missing something in how it is being called from the index page.
我的__construct的这一行代码没有错误,但我不希望在类中分配变量.
This line of code for my __construct works w/o error but I do not want the variable assigned in my class.
public function __construct(){
$this->user_id = '235454';
$this->user_type = 'Full Time Employee';
}
这是我的课程
<?php
class User
{
protected $user_id;
protected $user_type;
protected $name;
public $first_name;
public $last_name;
public $email_address;
public function __construct($user_id){
$this->user_id = $user_id;
$this->user_type = 'Full Time Employee';
}
public function __set($name, $value){
$this->$name = $value;
}
public function __get($name){
return $this->$name;
}
public function __destroy(){
}
}
?>
这是我的索引页中的代码:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$employee_id = new User(2365);
$employee_type = new User();
echo 'Your employee ID is ' . '"' .$employee_id->user_id. '"' . ' your employement status is a n ' . '"' .$employee_type->user_type. '"';
echo '<br/>';
?>
推荐答案
问题是:
$employee_type = new User();
构造函数需要一个参数,但是您什么也不发送.
the constructor expect one argument, but you send nothing.
更改
public function __construct($user_id) {
到
public function __construct($user_id = '') {
查看输出
$employee_id = new User(2365);
echo $employee_id->user_id; // Output: 2365
echo $employee_id->user_type; // Output: Full Time Employee
$employee_type = new User();
echo $employee_type->user_id; // Output nothing
echo $employee_type->user_type; // Output: Full Time Employee
如果您有一个用户,则可以执行以下操作:
If you have one user, you can do this:
$employer = new User(2365);
$employer->user_type = 'A user type';
echo 'Your employee ID is "' . $employer->user_id . '" your employement status is "' . $employer->user_type . '"';
哪个输出:
Your employee ID is "2365" your employement status is "A user type"
这篇关于警告:缺少参数1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!