OOP is intimidating to a lot of developers because it introduces new syntax and, at a glance, appears to be far more complex than simple procedural, or inline, code. However, upon closer inspection, OOP is actually a very straightforward and ultimately simpler approach to programming. Before we go in details, let’s define important terms related to Object Oriented Programming. Class− Simply Class is a collection of member variables and its functions. Object−You define a class once and then make many objects that belong to it. Objects have state behavior and identity. Member functions and Variables− these are the functions and variables defined inside a class and are used to access object data. Inheritance– Simply creating a new class from the existing class. When a class is defined by inheriting existing function of a parent class then it is called inheritance. Polymorphism– Using this you can create multiple functions with their same name. Or simply saying function name will remain same but it makes take different number of arguments and can do different task. Overloading− a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation. Data Abstraction− Any representation of data in which the implementation details are hidden (abstracted). Encapsulation− refers to a concept where we encapsulate all the data and member functions together to form an object. Constructor– A Constructor is the same name of its class, will automatic invoke once the object of class in created. Destructor– It refer to a special type of function which will be called automatically whenever an object is deleted or goes out of scope. Example: <?php // creating class called myClass class myClass { public $i; // creating function called myFn function myFn() { $c=10; echo “Value is “.$c; } } // Creating object for the class myClass $ob=new myClass(); // Calling function through class object $ob $ob->myFn(); ?>
Working with Images – PHP
We need to create an HTML form that allows you to choose the file to be uploaded. Note: please make sure you have form method type is post and enctype=”mutipart/form-data” to to handle file uploading process successfully. HTML Code <form enctype=”multipart/form-data” action=”process.php” method=”POST”> Choose File : <input name=”myfile” type=”file” /> <input type=”submit” value=”Upload” /> </form> Now create a PHP file process.php for handling file information <?php // get the uploaded file name $filename= $_FILES[‘myfile’][‘name’]; // get the file type $filetype= $_FILES[‘myfile’][‘type’]; // get the file size (in bytes) $filesize= $_FILES[‘myfile’][‘size’]; // You may put your validations here i.e restrict file size if($filesize>=1000000) { echo “ File is too Large, Maximum file size should be 1 MB”; exit; } // Validation to check if uploaded file is an Image if($filetype !=”image/jpeg” && $filetype !=”image/png” && $filetype !=”image/gif” && $filetype !=”image/JPEG” $filetype !=”image/PNG”) { echo “ Only Images allowed , Kindly upload valid Image.”; exit; } else { // create a folder – upload (in the same location where you have this PHP file) to move uploaded image from your local computer to server $target = “upload/”; $target = $target . basename( $_FILES[‘myfile’][‘name’]) ; move_uploaded_file($_FILES[‘myfile’][‘tmp_name’], $target); echo ” Image Uploaded Successfully”; } ?> You will be saving your uploaded images to a folder on the web server, which means you need a directory that is writable by the web server. To make the folder writable by the web server, you can use your FTP client for the same. To write the uploaded image to the target folder, you use the function move_uploaded_file. This PHP function will retrieve the image and move it to the designated location.