{64} - 3.6.2 Creating Custom Components

{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: 1-2Lines: 1-18
 <title>Creating Custom Components</title> <title>Creating Custom Components</title>
 +<p>
 + 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.
 +</p>
 +<p>
 + 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.
 +</p>
 +<pre>
 +&lt;?php
 +class MathComponent extends Object {
 + function doComplexOperation($amount1, $amount2) {
 + return $amount1 + $amount2;
 + }
 +}
 +?&gt;
 +</pre>