php 警告:缺少参数 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12452078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:36:58  来源:igfitidea点击:

Warning: Missing Argument 1

phpclass

提问by Michael Crawley

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.

我的 php 代码有一些问题:所有信息都返回,但我不知道为什么我会收到错误消息。对于我的索引页面,我只包含了实际使用该类的代码行,除了一些包含之外,确实没有其他代码。我确定这是我构建 __contstruct 的方式,但我不确定这样做的适当方式。我在如何从索引页面调用它时遗漏了一些东西。

This line of code for my __construct works w/o error but I do not want the variable assigned in my class.

我的 __construct 代码行没有错误,但我不希望在我的类中分配变量。

public function __construct(){
    $this->user_id = '235454';
    $this->user_type = 'Full Time Employee';


}

This is my Class

这是我的班级

<?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(){


}


 }

 ?>

This is my code from my index page:

这是我的索引页中的代码:

<?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/>';

 ?>

回答by Gabriel Santos

The problem is:

问题是:

$employee_type = new User();  

the constructor expect one argument, but you send nothing.

构造函数期望一个参数,但你什么都不发送。

Change

改变

public function __construct($user_id) {

to

public function __construct($user_id = '') {

See the outputs

查看输出

$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 . '"';

Which output:

哪个输出:

Your employee ID is "2365" your employement status is "A user type"

回答by Ian Andrew Irwin

I'm no PHP expert, but it looks like you are creating 2 new instances of class user, and on the second instatiation, you are not passing the user_id into the constructor:

我不是 PHP 专家,但看起来您正在创建类 user 的 2 个新实例,并且在第二次安装时,您没有将 user_id 传递给构造函数:

$employee_id = new User(2365);

This, it would seem to me, is creating a new instance of User and assigning this instance to the variable $employee_id - I don't think this is what you want though?

在我看来,这正在创建一个新的 User 实例并将这个实例分配给变量 $employee_id - 我不认为这就是你想要的?

$employee_type = new User();

This looks like you're instantiating another instance of User and assigning it to variable $employee_type - but you have called the constructor User() without passing in an ID as is required - hence the error (missing argument).

这看起来像是您正在实例化 User 的另一个实例并将其分配给变量 $employee_type - 但是您调用了构造函数 User() 而没有按要求传入 ID - 因此出现错误(缺少参数)。

The reason your return script contents look OK is because the first instance of the User class has an ID (because you passed it in) and the second one has an employee type because this is set in the constructor.

您的返回脚本内容看起来正常的原因是 User 类的第一个实例有一个 ID(因为您传入了它),而第二个实例有一个员工类型,因为它是在构造函数中设置的。

Like I say, I don't know PHP but I'm guessing you want something more along the lines of:

就像我说的,我不知道 PHP,但我猜你想要更多的东西:

$new_user = new User(2365);
echo 'Your employee ID is ' . '"' .$new_user->user_id. '"' . ' your employement status is a n ' . '"' .$new_user->employee_type. '"';

Here, you are instantiating a single instance of your user class assigned to the variable $new_user, and then accessing the properties of that single instance.

在这里,您正在实例化分配给变量 $new_user 的用户类的单个实例,然后访问该单个实例的属性。

EDIT: .....Aaaaaaaaand - I was too slow :-)

编辑:.....Aaaaaaaaand - 我太慢了:-)