-2

I have a problem here on PHP OOP. I try to do something that I always do in .NET - pass the whole object to the function. Unfortunately, the script didn't appear to work and when I try to debug (using Netbeans) it stopped here:

$ud = new userdetails($fullname, $email, $contact, $username, $password, $password2); 

Can somebody tell me what did I do wrongly? Thanks in advance!

My script:

<?php
include 'class/registration.php';

$fullname = $_POST['fullname'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$username = $_POST['username']; 
$password = $_POST['password'];
$password2 = $_POST['password2'];

$ud = new userdetails($fullname, $email, $contact, $username, $password, $password2);

if (registration::checkEmptyField($ud)==true){
        $error = "Please don't leave any field empty";
    } 

userdetail class:

<?php
class userdetails {

protected $_fullname;
protected $_email; 
protected $_contact; 
protected $_username;   
protected $_password; 
protected $_password2;

public function __construct($fullname,$email,$contact,$username,$password,$password2) {   
    $this->_fullname = $fullname;    
    $this->_email = $email;  
    $this->_contact = $contact;  
    $this->_username = $username;  
    $this->_password = $password;  
    $this->_password2 = $password2;  
}    

public function get_fullname() {    
    return $this->_fullname;          
}     

public function get_email() {    
    return $this->_email;          
}  

public function get_contact() {    
    return $this->_contact;          
}  

public function get_username() {    
    return $this->_username;          
}  

public function get_password() {    
    return $this->_password;          
}  

public function get_password2() {    
    return $this->_password2;          
}  

}

registration class:

<?php
class registration {

 function checkEmptyField(&$userdetails){   

     if ($userdetails-> get_fullname == ''){
         return true;
     }       
     elseif ($userdetails->get_email == ''){
         return true;
     }    
     elseif ($userdetails->get_contact == ''){
         return true;
     }    
     elseif ($userdetails->get_username == ''){
         return true;
     }    
     elseif ($userdetails->get_password == ''){
         return true;
     }    
     elseif ($userdetails->get_password2 == ''){
         return true;
     }

   }

 }
FirdhausKM
  • 85
  • 1
  • 9
  • 2
    You do not have to use & reference. – sectus Nov 27 '14 at 13:43
  • 1
    add "static" function checkEmptyField($userdetails) since you are not instantiating the object – Dawid O Nov 27 '14 at 13:45
  • Even though this is wrapped up in a class, this is just procedural programming only even harder to maintain or read. Proper answer has been posted, but you should really turn this into a proper OO paradigm. – N.B. Nov 27 '14 at 13:46
  • thanks for the comment, by the way, why did i get thumbs down here? i did few research, try n error but I can't find a way.. – FirdhausKM Nov 28 '14 at 04:58

2 Answers2

3

You ask for a property, not a method here: $userdetails-> get_fullname

Correct way: $userdetails-> get_fullname()

You should always turn on the error reporting, because this should have been reported by php.

Mārtiņš Briedis
  • 17,396
  • 5
  • 54
  • 76
0

The way you call registration::checkEmptyField() requires it to be declared as static.

<?php
class registration {

    static function checkEmptyField(userdetails $userdetails) {
       ...
    }
}

There is no need to prepend $userdetails with &, in PHP the objects are always passed by reference. It's better to use type hinting: prepend the parameter name ($userdetails) with its expected type (class userdetails in this case).

axiac
  • 68,258
  • 9
  • 99
  • 134