0

What is class and functions in PHP script or object oriented programming

What is Class in Programming ?

A class is a collection of variables and functions working with these variables. Variables are defined by var and functions are defined by function. It is an object oriented programming.

Define a class

Define a class using a keyword class

Syntax

class className

{

}

Example

class myfirstprogram

{

}

Note: Here myfirstprogram is a class name

Functions

Functions are defined inside the class, which is used to access the object data.

Define a function

Define a function using the keyword function

Syntax

function functionName()

{
  
}

Example

function factorial()

{

}

Note: factorial is a function name

Sample code to find factorial of the given number

//fact.php

<?php
class fact
{
public $num=null;
public $fact1=null;
public $objRes=null;

function getValue()
{
$this->num=$_REQUEST[‘num’];
}

function factorial()
{
if(isset($_REQUEST[‘submit’]))
{
$this->getValue();
$fact1=1;
for ($i=$this->num; $i>=1; $i–) {
$fact1 = $fact1 * $i;
$this->objRes=$fact1;
}
}
}
}
$objFact=new fact();
$objFact->factorial();

?>
<html>
<body>
<form action=”fact.php” method=”post”>
Enter the number<input type=”text” name=”num”/>
<input type=”submit” name=”submit”/>
</form>
<?php
echo “Factorial of $objFact->num is $objFact->objRes”;
?>
</body>
</html>

output

fact

Filed in: Uncategorized

Recent Posts

Bookmark and Promote!

Leave a Reply

Submit Comment
© 2017 Expert Shout. All rights reserved. XHTML / CSS Valid.