Warning: Missing Argument 1

2019-02-14 00:02发布

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.

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


}

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

 ?>

标签: php class
2条回答
Deceive 欺骗
2楼-- · 2019-02-14 00:31

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:

$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?

$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).

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.

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

$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.

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

查看更多
淡お忘
3楼-- · 2019-02-14 00:49

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"
查看更多
登录 后发表回答