Where in the world does this variable come from?

Permalink
I have been studying the code for the Guestbook block and as always in the code, there are variables that seem to come out of nowhere as if by magic.

This makes the code incredibly hard to follow and dare I say poorly written in that it is missing comments explaining these things which would have helped a lot.

Anyway can someone please fill me in on where the $controller class variable is instantiated?

Inside the view.php file of the Guestbook block (located inside /concrete/blocks/guestbook/) there is the following line where $controller is first used in the file.

.guestBook-formBlock span.error, div#guestBook-formBlock-<?php echo $controller->bID?> span.error {
   color:#990000;
   text-align:left;
}


It's part of the CSS at the top of the file.

Now obviously $controller has been defined and instantiated before the code in this file is ever reached but given the absolute lack of comments inside view.php about what this $controller is and what the bID is can someone please fill me in on this information here?

I am tired of searching through hundreds of lines of codes for "$controller" trying to make heads or tails of which controller instantiation is the one I am looking at and am hoping someone can fill me in on which file has the instantiation and what it is doing inside the block Guestbook view file?

Thanks.

Carlos

 
12345j replied on at Permalink Reply
12345j
its not meant to be an actual variable, its just looking in the controller.php file to get this blocks id. Its like how you'd call functions in view.php with $this->getFunction() and from controller.php with $controller->getFunction() bID is the blocks id, which is used to link the block tables to the actual page/block.
carlos123 replied on at Permalink Reply
Thanks for the input 12345.

Hmm...well, I know that $controller->bID is accessing the bID property of the controller class. That's pretty obvious from the construct used.

But what I don't get is where the controller class being accessed is instantiated and how it is that the GuestBook view.php file has access to that instantiation given that it's nowhere in sight?

Obviously some code has run somewhere to allow view.php to have access to the controller class but where is that code?

Where is the code that is wrapping the code inside view.php?

I am trying to follow the C5 code and with variables, instantiations, methods, and properties spread around all over the place and appearing from out of nowhere it's getting real hard to follow the code at all.

I know that I might seem a gluten for punishment trying to understand things this way but that is what I am trying to do.

Thanks.

Carlos
andrew replied on at Permalink Best Answer Reply
andrew
$controller is actually set in all block views by this line, which happens in concrete/libraries/block_controller.php, which all block type controller's extend:

public function __construct($obj = null) {
/* snip */
    parent::__construct();
    $this->set('controller', $this);
}


$controller is the class mapping to the particular instance of the block. That's why it has a block ID ($bID) which is referenced as $controller->bID. Usually we reference this using $controller->getBlockID() (I agree, the guestbook block is pretty sloppy in its coding.)

If you want to see how these view files are rendered and how the various sets are extracted from the controller files into the view, check out concrete/libraries/block_view.php.
carlos123 replied on at Permalink Reply
Thanks very much Andrew! That's exactly the kind of input I was looking for.

Carlos