jeeeee
jeeeee

Reputation: 229

CakePHP $this Syntax

I've been trying to learn CakePhp for a while now but I still can't get alot of stuff. I've been reading a lot and watching videos. I just want to ask a very simple question.

I've been trying to mess with the bookmarks tutorial and i'm watching a video. In the video he baked a component called Validate. In the cmd he typed

bin/cake/bake component Validate

Then a ValidateComponent.php appeared in the component folder in the controller. Now he used the ValidateComponent.php by going to the BookmarksController and adding to the initialize method

$this->loadComponent('Validate');

I just want to ask where did the word validate come from? shouldn't it be ValidateComponent? and where does he get the loadComponent from? I've seen him using $this->method(); or $this->method('string', [array]); I just want to know how the syntax works and what each word means. Sorry for the long explanation. I'm really trying to learn and i'm really confused. thank you very much.

ValidateComponent.php

    <?php
namespace App\Controller\Component;

use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;

/**
 * Validate component
 */
 class ValidateComponent extends Component
{

/**
 * Default configuration.
 *
 * @var array
 */
protected $_defaultConfig = [];

public function validLimit($limit, $ default)
{
    if (is_numeric($limit)){
    return $limit;
}
return $default;
}

}

part of BookmarksController.php

 public function initialize()
{
    parent::initialize();

    $this->loadComponent('Validate');

}

I can't seem to find where he got the word 'Validate'

Upvotes: 1

Views: 717

Answers (2)

devnull
devnull

Reputation: 1928

Every controller in your application extends a base Controller Controller or AppController which extends Controller

Controller have bunch of methods, One of these methods is the loadComponent() (See Source)

public function loadComponent($name, array $config = [])
     {
         list(, $prop) = pluginSplit($name);
         $this->{$prop} = $this->components()->load($name, $config);
     }

Why Validate instead of ValidateComponent?

Short answer: suffix.

See predefined suffix in App class

CakePHP uses suffix to load classes, When you hit loadComponent() You go to ComponentRegistery class to Register the component, ComponentRegistery will call App class to load class. __loadClass()

Almost everything in CakePHP has a suffix, In your case ValidateComponent has the Component suffix.

return App::className($class, 'Controller/Component', 'Component'); (Source)

I hope this will make more sense to you

Upvotes: 2

Doug
Doug

Reputation: 1890

$this isn't specifically anything to-do with cake but part of PHP itself. In object oriented context $this refers simply to the current class.

$this->something refers to an object within the current scope. This could be within the current class or from an extends or use.

$this->something(); refers similarly to a method or function within the current scope.

If are using an IDE such as netbeans you can usually click through these references to see the object they refer to so for example if you do in fact use Netbeans you could ctrl-click on $this->loadComponent('Validate'); to see the actual function it refers to.

Regarding where does 'Validate' come from, it's a string you are passing to that object. On the other end it will be used in the function, probably in a switch or if statement to return something.

Eg:

Public function loadComponent($type){
    If($type == 'Validation'){
     //do something
    }
}

Upvotes: 1

Related Questions