Welcome Guest, Not a member yet? Register   Sign In
Is my library function from common file a good practice ?
#1

(This post was last modified: 06-30-2020, 01:31 PM by cvlancvlan.)

I created this library function in app/Common.php because i want to call easier my libraries but i don't know if this function is a good practice for codeigniter 4. Please tell me if this function is a good practice.

app/Common.php
PHP Code:
if ( !function_exists('library') ) {
    
    function 
library(string $name, ...$params) {
        
$library '\App\Libraries\\' ucfirst($name) . 'Library';
        return new 
$library(...$params);
    }
    



app/Controllers/Home.php
PHP Code:
<?php namespace App\Controllers;

class 
Home extends BaseController {
    
    public function 
index() {
        echo 
library('shop')::displayPrice(25.000) . '<br>';
        echo 
library('shop')->priceFormat(30.000) . '<br>';
        
        
$shop library('shop');
        echo 
$shop::displayPrice(40) . '<br>';
        echo 
$shop->priceFormat(50);
    }



app/Libraries/ShopLibrary.php
PHP Code:
<?php namespace App\Libraries;

class 
ShopLibrary {
    
    public static function 
displayPrice($price) {
        return (new 
self)->priceFormat($price);
    }
    
    public function 
priceFormat($price) {
        return 
number_format($price2'.''');
    }
    

Reply


Messages In This Thread
Is my library function from common file a good practice ? - by cvlancvlan - 06-30-2020, 01:22 PM



Theme © iAndrew 2016 - Forum software by © MyBB