dsadsa123111 2014-06-23 22:01
浏览 56

从child调用时从PHP父方法返回值

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);
         }
}
  • 写回答

1条回答 默认 最新

  • dongshan4549 2014-06-24 00:02
    关注

    I needed a return in the child method:

    return parent::isRegistered($this->email, $this->dbconn);

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?