Composer and OOPHP, a great combination for php application. This combination not only reduce your work pressure but also give you a nice clean and helpful application development system.
In this article , I will try to show how we can create a very simple project with OOPHP and composer. Our project is Simple Calculator.
What is Composer ?
I know most of the developer already know about this, but some beginner like me still learning about this. It is a tool for dependency management in PHP. It allows you to declare
the libraries your project depends on and it will manage (install/update) them
for you. To know more about Composer , click here .
What is OOPHP?
OOPHP is Object Oriented Programming with PHP. In the internet , there are lots of tutorial to learn this. I am just a beginner , so , I am not trying to teach you OOPHP. Instead , I just sharing a very simple project that developed with OOPHP and composer is used on it.
This project is a simple calculator, it just take some input from you and give you a result based on your data and required operation. Here is a screenshot of my project -
Lets start the project . First install composer on your system from composer website. Before everything , we need to create folder structure. My folder structure for this project is -
Folder Structure:
Root
|- Asset
|----|--css
|----|--fonts
|----|--js
|- Vendor
|- src
|----|--calculator
In the "Asset" folder , I kept the theme files and folder like css , images, js.
"Vendor" folder is for composer, I will tell about this in Composer section later.
"src" folder is the folder where I kept my class files that required for OOP.
Now , we need a composer file. The name of the composer file is "composer.json":
Code in Composer.json file:
___________________________
{
"autoload": {
"psr-4":{
"phpCalc\\":"src"
}
}
}
_________________________
Here , "autoload" is for loading the classes automatically and "psr-4" describes a specification for autoloading classes from file
paths. To know more about "psr" , click here .
Now , "phpCalc" is the name of our application and "src" is the folder where our class files are existed. Now , to use composer.json file we need 2 more thing , one is vendor folder and another is terminal command. in "Vendor" folder composer will keep its necessary files, it will create by itself.
And the terminal command for the composer is :
#composer dump-autoload
Just open your terminal or command prompt on your root folder and use the command , it will complete all necessary step automatically . So, our composer is ready and we will use it after creating our class file so that it can gather all class information .
Now lets create class file. I created a directory inside "src" folder called "calculator" and then created a class file in this folder called "calculator.php" .
Codes inside the "calculator.php" file :
________________________________________
<?php
// Here namespace is so much important for composer
namespace phpCalc\calculator;
class calculator
{
public $num1="";
public $num2="";
public function __construct($argument1,$argument2){
$this->num1=$argument1;
$this->num2=$argument2;
}
public function add(){
$result=$this->num1." + ". $this->num2 ." = ";
$result .=$this->num1+$this->num2;
return $result ;
}
public function multiply(){
$result=$this->num1." X ". $this->num2 ." = ";
$result .=$this->num1*$this->num2;
return $result ;
}
public function divide(){
$result=$this->num1." / ". $this->num2 ." = ";
$result .=$this->num1/$this->num2;
return $result ;
}
public function subtract(){
$result=$this->num1." - ". $this->num2 ." = ";
$result .=$this->num1-$this->num2;
return $result ;
}
}
?>
______________________________________________
I think you already know about class file and oop, so I don't need to explain details about these codes because I tried to make these codes as simple as I can.
Now we will use this class (calculator). I used this class in "index.php" file . All operation result will be shown in index page .Here is my "index.php" files codes.
Codes of "index.php" file:
___________________________________________________
<?php
// inside "vendor" folder , there is a "autoload.php" file . It created by composer to automatically load classes. We have to include this file here ...
include_once 'vendor/autoload.php';
use phpCalc\calculator\calculator; // Calling the class "calculator"
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="asset/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="jumbotron"> A Simple Calculator using OOPHP</div>
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<form action="index.php" method="post" role="form">
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<label for="number1">Number 1:</label>
<input type="number" name="num1" class="form-control" id="number1">
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label for="number2">Number 2:</label>
<input type="number" name="num2" class="form-control" id="number2">
</div>
</div>
</div>
<button type="submit" name="add" class="btn btn-default">Add</button>
<button type="submit" name="subtract" class="btn btn-default">Subtract</button>
<button type="submit" name="multiply" class="btn btn-default">Multiply</button>
<button type="submit" name="divide" class="btn btn-default">Divide</button>
</form>
</div>
<div class="col-md-6">
<div class="col-md-12">
<h2>Result:</h2>
<?php
$text="";
if(isset($_POST['num1']) && isset($_POST['num2'])){
$calculator = new calculator($_POST['num1'],$_POST['num2']);
if(isset($_POST['add'])){
$text=$calculator->add(); // Calling add() method from class
}elseif(isset($_POST['subtract'])){
$text=$calculator->subtract();// calling subtract() method from class
}elseif(isset($_POST['multiply'])){
$text=$calculator->multiply();// calling multiply() method from class
}elseif(isset($_POST['divide'])){
$text=$calculator->divide();// Calling divide() method from class
}
}
?>
<textarea readonly="readonly"><?= $text ?></textarea>
</div>
</div>
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="asset/js/jquery.min.js"></script><!-- I downloaded jquery file and placed it inside asset/js folder.-->
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="asset/js/bootstrap.min.js"></script>
</body>
</html>
_________________________________________
Thats it, the project is done. Now open your terminal or command prompt from root folder and run the command -
#composer dump-autoload
Your project is ready.
I know , I am not much good blogger . Suggest me to improve my blogging and comment if you have any query.
Thanks for reading.

No comments:
Post a Comment