PHP OOP -> Basic Structure

<?php // This script explains php object oriented programming concepts. // Creating class, Creating objects, Creating methods or functions, //Declaring variables and doing basic artithmetic. // Note : You should have basic knowledge in php, for running php scripts to // understand these codings. class Basic { //variable declaration. var $num1 = 3; var $num2 = 3; var $result; //method or function declaration function basicAdd() { $result = $this->num1+$this->num2; //Omit $ when accessing variable. echo "Addition of 3 + 3 is : " .$result; echo "<br>"; // For line break between results. } function basicSub() { $result = $this->num1-$this->num2; //Omit $ when accessing variable. echo "Subtraction of 3 - 3 is : " .$result; echo "<br>"; // For line break between results. } function basicMul() { $result = $this->num1*$this->num2; //Omit $ when accessing variable. echo "Multiplication of 3 * 3 is : " .$result; echo "<br>"; // For line break between results. } function basicDiv() { $result = $this->num1/$this->num2; //Omit $ when accessing variable. echo "Division of 3 / 3 is : " .$result; echo "<br>"; // For line break between results. } } // Class scope ends here. // Object creation and accessing methods MUST be outside of class // like this example. If you put inside class it will fire ERROR parse - // expecting T-Varaible. // Creating object from class. $abc = new Basic; //Accessing methods through objects. $abc->basicAdd(); $abc->basicSub(); $abc->basicMul(); $abc->basicDiv(); //Output is //Addition of 3 + 3 is : 6 //Subtraction of 3 - 3 is : 0 //Multiplication of 3 * 3 is : 9 //Division of 3 / 3 is : 1 ?>

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.