我已经充分使用了 php 以适应它,但最近我一直在研究一些 MVC 框架以尝试理解它们是如何工作的,并且我遇到了我没有的语法和数据结构'之前遇到过:
function view($id)
{
$this->Note->id = $id;
}
此代码的 ->id 部分是什么?这是基于其父方法的子方法吗?如果是这样,我该如何编写代码来创建这样的结构? (即从头开始创建结构,而不是使用现有框架,如上面来自 cakephp 的示例)。
最佳答案
以下代码演示了如何获得您描述的结构。
<?php
class Note
{
public $id = 42;
}
class MyClass
{
public function __construct() {
// instance of 'Note' as a property of 'MyClass'
$this->Note = new Note();
}
public function test() {
printf("The \$id property in our instance of 'Note' is: %d\n",
$this->Note->id);
}
}
$mc = new MyClass();
$mc->test();
?>
关于PHP 子方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4971804/