开发者

Php sub-methods

I've used php enough to be quite comfortable with it, but recently I've been looking through some MVC frameworks to try a开发者_StackOverflownd understand how they work, and I've come across a syntax and data structure which I haven't encountered before:

function view($id)   
   {   
       $this->Note->id = $id;   
   }

What is the ->id section of this code? Is this a sub-method based off it's parent method? If so, how do I go about writing code to create such a structure? (ie. creating the structure from scratch, not using an existing framework like the above example from cakephp).


The following code demonstrates how one could arrive at the structure you described.

<?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();
?>


Note is a property of $this and it's (current) value is an object with a property named id which gets assigned the value of $id.
If id was a method of the Note object, the line would read $this->Note->id($id);.


Another way to think about the construct is considering

 $this->Note->id = $id;

similar to

 $this["Note"]["id"] = $id;

Which would actually be equivalent if both objects ($this and subobject Note) were based on ArrayAccess.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜