I am new to PHP OOP so I am not sure if I am going about this the right way. The parent method, isRegistered(), will query the db correctly and return a value ($affected_rows) when I call the method from the parent, but will only "echo" the value (will not return it) when I call the method from the child, so I am not sure if I have constructed the child incorrectly. Here is my code:
//Log in with social credentials
if(isset($_POST["fb-reg"])) {
$fbOptions = array(
'email' => "joe@gmail.com",
'fbId' => "678910",
'handler' => $handler); //the db connection
fbRegister($fbOptions);
}
//Instantiate parent and child and get results of query
function fbRegister($fbOptions) {
$a = new newUser();
$fb = new fbUser($fbOptions);
$fb->isRegistered(); // will echo back $affected_rows
$y = isRegistered();
echo $y; // does not return value of $affected_rows
}
//Parent class
class newUser {
public $email;
public $pass;
public $dbconn;
public $confirm;
public $message;
public function __construct($options) {
$this->email = $options['email'];
$this->pass = $options['password'];
$this->confirm = $options['confirm'];
$this->dbconn = $options['handler'];
$this->message = array();
}
// CHECK IF USER EXISTS ====================================
public function isRegistered() {
$sql=("SELECT * FROM Test WHERE Email = :email");
$query = $this->dbconn->prepare($sql);
$query->execute(array(':email' => $this->email));
$affected_rows = $query->rowCount();
return $affected_rows; // query works but does not return value to calling function
echo $affected_rows; // query result will be echoed by calling function
}
}
//Child class
class fbUser extends newUser{
public $id;
public $email;
public $dbconn;
function __construct($fbOptions) {
$this->id = $fbOptions['fbId'];
$this->email = $fbOptions['email'];
$this->dbconn = $fbOptions['handler'];
}
public function isRegistered() {
parent::isRegistered($this->email, $this->dbconn);
}
}