CodeIgniter Forums
Is my library function from common file a good practice ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Is my library function from common file a good practice ? (/thread-76894.html)



Is my library function from common file a good practice ? - cvlancvlan - 06-30-2020

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'.''');
    }
    




RE: Is my library function from common file a good practice ? - ivantcholakov - 06-30-2020

No, it is not.

1. You are making your own unique loading system that somebody else should get used with. Also, this system makes your code less portable.
2. Your function always returns a new instance. Is this always needed? Sometimes singletons are more appropriate.
3. In your sample usage code how do you decide when to use static or dynamic methods? It looks messy.

Edit: You don't need one "good practice", you need a "bag of tricks", a repertoire on how to create objects and on how to access them easily. Object creation depends on the context, and on object's purpose. There are known patterns for these things, get familiar with these concepts when you have the time.

While your project is not too demanding try to stick with what CodeIgniter has got now: https://www.youtube.com/watch?v=iqQufhllUCo

If things get complicated too much and what CodeIgniter proposes is not enough, maybe then you would adopt a more complicated solution that would require more experience. But don't try to invent one. See for example https://php-di.org/ , https://github.com/PHP-DI/PHP-DI


RE: Is my library function from common file a good practice ? - cvlancvlan - 06-30-2020

(06-30-2020, 04:13 PM)ivantcholakov Wrote: No, it is not.

1. You are making your own unique loading system that somebody else should get used with. Also, this system makes your code less portable.
2. Your function always returns a new instance. Is this always needed? Sometimes singletons are more appropriate.
3. In your sample usage code how do you decide when to use static or dynamic methods? It looks messy.

Edit: You don't need one "good practice", you need a "bag of tricks", a repertoire on how to create objects and on how to access them easily. Object creation depends on the context, and on object's purpose. There are known patterns for these things, get familiar with these concepts when you have the time.

While your project is not too demanding try to stick with what CodeIgniter has got now: https://www.youtube.com/watch?v=iqQufhllUCo

If things get complicated too much and what CodeIgniter proposes is not enough, maybe then you would adopt a more complicated solution that would require more experience. But don't try to invent one. See for example https://php-di.org/ , https://github.com/PHP-DI/PHP-DI

Thank you so mouch @ivantcholakov
Here i set public for priceFormat just for call library with static and public method

I'm beginner in ci4 and i want learn this framework because i like so much. With some days a go i started documentations about this codeigniter 4 and i have tons of questions... 

I want use this ShopLibrary in all controllers. Which is the best solution ?
If you know other solution for "autoload" library please put here.

app/Config/Services.php
PHP Code:
<?php namespace Config;
use 
CodeIgniter\Config\Services as CoreServices;
require_once 
SYSTEMPATH 'Config/Services.php';

class 
Services extends CoreServices {
    
    
#For method 1
    
public static function shopLibrary($getShared true) {
        if (
$getShared) {
            return static::
getSharedInstance('shopLibrary');
        } return new \
App\Libraries\ShopLibrary();
    }




app/Controllers/BaseController.php
PHP Code:
<?php
namespace App\Controllers;
use 
CodeIgniter\Controller;

class 
BaseController extends Controller {

    protected 
$helpers = [];

    public function 
initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger) {
        
        
parent::initController($request$response$logger);
        
#For method 2 but i don't know if this is a good solution
        
$this->shopLibrary = new \App\Libraries\ShopLibrary();
        
    }



app/Controllers/Home.php
PHP Code:
<?php namespace App\Controllers;
use 
App\Libraries\ShopLibrary#For method 3 (but i not like to add this line in all controllers)
class Home extends BaseController {
    
    
public function index() {
        
        
#Method 1
        
$Method1 service('shopLibrary');
        echo '<br>' $Method1->priceFormat(10);
        echo '<br>' $Method1::displayPrice(20);
        
        
#Method 2
        
$Method2 $this->shopLibrary;
        echo '<br>' $Method2->priceFormat(30);
        echo '<br>' $Method2::displayPrice(40);
        
        
#Method 3
        
$Method3 = new ShopLibrary();
        echo 
'<br>' $Method3->priceFormat(50);
        echo '<br>' $Method3::displayPrice(60);
        
        #I don't know other method...

    }
    




RE: Is my library function from common file a good practice ? - ivantcholakov - 07-01-2020

Try this

Code:
$this->shopLibrary = \Config\Services::shopLibrary();

This way you will have a singleton. And since you have an instance, avoid using static methods, because they will force you to remember and recall what is the full namespace of the library, which is not convenient.

And we all know it is a library, maybe just "shop" would be nicer than "shopLibrary". Edit: Or "shopping" if you want to avoid a future name collision.


RE: Is my library function from common file a good practice ? - cvlancvlan - 07-02-2020

Thank you @ivantcholakov