Welcome Guest, Not a member yet? Register   Sign In
Creating a custom service/library
#4

(This post was last modified: 02-05-2020, 04:31 PM by littlej.)

Hello Fido L Dido !

I have an application in which I needed to manage measurement conversions (inches to centimeters, meters to feet, inches to feet...). So I created an independent library, because I can use it in other apps. And it does not use a database.

1 / The class is located here: "app/Libraries/Snippets/conversion.php". It uses the namespace: "namespace Snippets;" (at the beginning of the file, just after "<?php "), which is the folder's name.
2 / In the file "app/Config/autoload.php" I added the namespace to the $psr4 variable:

PHP Code:
$psr4 = [
  'Config'      => APPPATH 'Config',
  APP_NAMESPACE => APPPATH,                // For custom namespace
  'App'         => APPPATH,                // To ensure filters, etc still found,
  'Snippets'    => APPPATH 'Libraries/Snippets' // The namespace I use in step 1 => The folder's location
]; 

3 / When I need it, I use it like this:

PHP Code:
$conversion = new \Snippets\Conversion();
$lenghtConversion $conversion->lengthimperialtometric(10,'in','cm');

// "Conversion()" is the name of the class 
// The method "lengthimperialtometric" requires: a value (10), the initial unit (inches), the desired unit (centimeters)
// The result is: $lengthConversion = 25.4; 

Hope it helps you find a solution ;-)
Reply


Messages In This Thread
RE: Creating a custom service/library - by x1250 - 02-05-2020, 08:30 AM
RE: Creating a custom service/library - by littlej - 02-05-2020, 04:07 PM



Theme © iAndrew 2016 - Forum software by © MyBB