Excellent Programming Tricks

PHP Composer | Namespaces | Add PHP Libary



Composer - Manage library and dependency of files and program.
Download and install - https://getcomposer.org.
Directory - Go to the specific folder in Command prompt. Like C:\>cd C:\xampp\htdocs\project3\wp-content\plugins\iftekhar-plugin.

  1. Before Install (Optional)
    1. Set path (D:\xapmm\php)
      1. Environment Variables->System Variables->Path->New->(D:\xapmm\php)New->OK  
    2. Check PHP status(Version)
      1. C:\Users\iftekhar>php -v
  2. Install
    1. https://getcomposer.org/ -> Getting Started -> Download Composer-Setup.exe.
    2. Finish Install.
    3. Write composer in command prompt and displays all composer command if successfully installed. 
    4. If composer install  isn't possible then add vendor folder in the project folder.  
  3. Add projects with composer | Commands
    1. C:\>cd C:\xampp\htdocs\...\iftekhar-plugin(sample)>composer init
      1.  Welcome to the Composer config generator , This command will guide you through creating your composer.json config.
      2. Package name (<vendor>/<name>) [dell_bwdb/iftekhar-plugin]:Press enter
      3. Description:  Add Description
      4. Author: Press enter [automatically recognize]
      5. Minimum Stability: dev
      6. Package Type: Project
      7. License: MIT/GPL
      8. Dependencies: no(built in composer auto load)[Yes when need packages]
      9. Dev dependencies: no.
      10. Confirm generation: yes [Finished]
      11. Now it's generate: compose. json file.
  4. Add Auto loading options [For example we use Reference folder name Inc and inc is the folder]
    1. Add auto loader in composer.json
      1. After "require"{}, add->  "autoload": {
                "psr-4": {"Inc\\": "./inc"} //it locates inc folder using Inc.
            }
      2. C:\>cd C:\xampp\htdocs\...\iftekhar-plugin>composer install
    2. Edit
      1. C:\>cd C:\xampp\htdocs\...\iftekhar-plugin>composer dump-atuoload 
  5. Don't edit anything inside vendor file everything manage by json flie.
  6. Only one require_once after using autoload | Use in parent file (index.php or abc.php)
    1. if ( file_exists( dirname( __FILE__ ) . '/vendor/autoload.php' ) ) {
       require_once dirname( __FILE__ ) . '/vendor/autoload.php';
      }
    2. Add  "use Inc\Activate;'' line in parent file (use backward slash).
    3. Reference page or child page or other page
      1. Namespace like: namespace  Inc;
      2. psr-4 convention:  Class name (class Iftekhar{...} ) and page mane should be similar(Iftekhar.php).  


    No comments:

    Post a Comment


    Authentic аnd Excellent

    Website

    HTML Template

    Wordpress Theme

    Database applications

    OR

    Application services?

    Excellent Programming Tricks (EPT) || Iftekhar-IT || We develops the Web applications and the WordPress templates. || Excellent Programming Tricks (EPT)

    © 2020 Blogger Theme by Iftekhar IT || Excellent Programming Tricks

    Execllent Programming Tricks. Powered by Blogger.