{376} - 3.6.2 Creating Custom Components

Suppose our online application needs to perform a complex mathematical operation in many different parts of the application. We could create a component to house this shared logic for use in many different controllers.

The first step is to create a new component file and class. Create the file in /app/controllers/components/math.php. The basic structure for the component would look something like this.

<?php

class MathComponent extends Object {
    function doComplexOperation($amount1, $amount2) {
        return $amount1 + $amount2;
    }
}

?>
  1. <?php
  2. class MathComponent extends Object {
  3. function doComplexOperation($amount1, $amount2) {
  4. return $amount1 + $amount2;
  5. }
  6. }
  7. ?>

Once our component is finished, we can use it in the application’s controllers by placing the components name in the controller’s $components arrays.

//Make the new component available at $this->Math

var $components = ('Math', 'Session');
  1. //Make the new component available at $this->Math
  2. var $components = ('Math', 'Session');

{3819} - 3.6.2 Creating Custom Components

Suppose our online application needs to perform a complex mathematical operation in many different parts of the application. We could create a component to house this shared logic for use in many different controllers.

The first step is to create a new component file and class. Create the file in /app/controllers/components/math.php. The basic structure for the component would look something like this.

<?php

class MathComponent extends Object {
    function doComplexOperation($amount1, $amount2) {
        return $amount1 + $amount2;
    }
}

?>
  1. <?php
  2. class MathComponent extends Object {
  3. function doComplexOperation($amount1, $amount2) {
  4. return $amount1 + $amount2;
  5. }
  6. }
  7. ?>

Differences

Lines: 14-26Lines: 14-18
  }  }
 } }
 ?&gt; ?&gt;
-</pre> 
-<p> 
- Once our component is finished, we can use it in the application’s controllers by placing the components name in the controller’s $components arrays. 
-</p> 
-<pre> 
-//Make the new component available at $this-&gt;Math 
-var $components = ('Math', 'Session'); 
 </pre> </pre>