Php 选择菜单对象
我正在尝试创建一个可重用的选择菜单对象: `班主任{Php 选择菜单对象,php,oop,tostring,Php,Oop,Tostring,我正在尝试创建一个可重用的选择菜单对象: `班主任{ var $nid; var $level_id; var $output; public function __construct($nid) { include 'con.php'; $stmt = $conn->prepare( 'SELECT a.uid, pp.fName, pp.lName FROM primary_profile as pp LEFT JOIN attributes as a ON pp.uid =
var $nid;
var $level_id;
var $output;
public function __construct($nid)
{
include 'con.php';
$stmt = $conn->prepare(
'SELECT a.uid, pp.fName, pp.lName FROM primary_profile as pp LEFT JOIN attributes as a ON pp.uid = a.uid WHERE nid = :nid AND :level_id = level_id');
$stmt->execute(array(':nid' => $nid, ':level_id' => 3));
$output .= '<select name="mentor_id">';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$output .='<option value="'.$row['uid'].'">'.$row['fName']. ' ' .$row['lName'].'</option>';
}
$output .= '</select>';
return $output;
}
public function __toString(){
return $output;
}
var$nid;
var$level_id;
var$输出;
公共功能构造($nid)
{
包括“con.php”;
$stmt=$conn->准备(
'从主配置文件中选择a.uid、pp.fName、pp.lName作为pp.uid=a.uid上的pp左连接属性,其中nid=:nid和:level_id=level_id');
$stmt->execute(数组(':nid'=>$nid',:level_id'=>3));
$output.='';
而($row=$stmt->fetch(PDO::fetch\U ASSOC)){
$output.='.$row['fName'].'.$row['lName'].';
}
$output.='';
返回$output;
}
公共函数uuToString(){
返回$output;
}
}`
在我的网页上,我打电话:
$mentor\u select=新导师($nid);
echo$mentor_select代码>
如果我在类外的页面上放置,但在类中,我得到一个错误,则它是有效的:
可捕获的致命错误:方法Mentor::\uu toString()必须返回字符串值
我知道这意味着uuu toString必须输出一个字符串,但据我所知,$output是一个字符串
我是OOP新手。请帮助我解决我缺少的内容$output=null。您需要使用$this->output将其设置为函数中的类变量
public function __construct($nid)
{
$this->output .= '<select name="mentor_id">';
$this->output .= '</select>';
通常包括“db_或_config_file.php”;goes/可以在定义类的文件开头看到。构造函数不应该显式返回值。它们在实例化类时使用。我会给你发一个简单的例子,告诉你如何更好地使用你的课程。谢谢惠而风。。。我看过你的课,你的方式更合理。。。但是我不明白while循环是从哪里来创建菜单选项的,我仍然需要围绕构造函数来思考,我会尝试找到更多的教程,但信息似乎很少。创建菜单项和返回输出的好地方是getOutput方法。离开构造函数进行初始化和基本设置。我还需要更好地理解getter和setter,我将继续尝试并查找教程……嗨,David,我确实尝试过:$output.='';而($row=$stmt->fetch(PDO::fetch_ASSOC)){$output.='.'.$row['fName'.'.'.'..$row['lName'.'.';}$output.='';//返回$output;}公共函数{uuu-toString(){return$this->output;}}
但中途出现了相同的错误……`$output.='谢谢你的帮助David。。我可以看到,我仍然有很多的学习要做的事情,让OOP的事情在我的头脑中
public function __toString(){
return $this->output;
}
<?php
class Mentor {
//Members declared as private may only be accessed by the class that defines the member
//Also you can declare members of a class as protected, and public. Read about that.
private $nid;
private $level_id;
private $output;
public function __construct($nid)
{
$this->level_id = 0;//You can initialize properties to their default values here
$this->nid = $nid;
$this->output = "output value $this->nid";
//return $this->output; constructors should not return values explicitly
//they are used to instantiate the class
}
public function __toString(){
return $this->output;
}
public function getOutput(){
return $this->output;//property output is private, so we define a public method which allows us to read it's value outside this class.
}
}
$mentor = new Mentor(3);// you don't need to return value from your constructor, because you have defined __toString magic method.
echo $mentor;
?>