我有这个控制器
class PageController extends Controller
{
private $myid;
public funciton index(){
}
public function viewbyid($id){
$this->myid = $id;
return view('someview');
}
public function getRecord(){
$id = $this->myid;
echo $id; //it would be null here,if I am going to access this method.
return view('anotherview');
}
}
是的,你可以访问私有变量,在任何地方的侧类,你使用oop PHP。所以可能的问题是你可能访问getRecord()
方法与diff对象。
:
$obj=new PageController();
$obj->viewbyid("Test");
$obj->getRecord();//Then it will display the result
如果您重新初始化对象或创建新对象,那么该对象将重新分配内存,因此以前保存的值将不存在。
例如:
$obj=new PageController();
$obj->viewbyid("Test");
$obj=new PageController();//$obj will allocate memory in diff location so your previous values will be initialized to default.
$obj->getRecord();//Then it will display result as null