Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: Missing Argument 1

Tags:

php

class

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

 ?>
like image 586
Michael Crawley Avatar asked Sep 17 '12 01:09

Michael Crawley


1 Answers

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"
like image 99
Gabriel Santos Avatar answered Sep 17 '22 22:09

Gabriel Santos