Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between class and function in php? [closed]

Tags:

php

I have used the following codes my self, both of them gave the same result. Then why we are using class instead of a normal function, and what is the difference between classes and functions.

<?php

class MyClass{

    public $property = "This is My Class Property";

    public function getProperty(){

        echo $this->property."<br />";

    }   

}

$obj=new MyClass();
echo $obj->getProperty();

?>


<?php

    function getProp(){

        $prop="This is My Class Property";

        echo $prop;

    }

    getProp();

?>
like image 578
Manǒj κiψàη Avatar asked Dec 18 '13 04:12

Manǒj κiψàη


People also ask

What is the difference between class and function in PHP?

One of the big differences between functions and classes is that a class contains both data (variables) and functions that form a package called an: 'object'. Class is a programmer-defined data type, which includes local methods and local variables. Class is a collection of objects. Object has properties and behavior.

What is the difference between class and function?

Functions do specific things, classes are specific things. Classes often have methods, which are functions that are associated with a particular class, and do things associated with the thing that the class is - but if all you want is to do something, a function is all you need.

What is closure function PHP?

A closure is an anonymous function that can access variables imported from the outside scope without using any global variables. Theoretically, a closure is a function with some arguments closed (e.g. fixed) by the environment when it is defined. Closures can work around variable scope restrictions in a clean way.

What's the difference between -> and :: in PHP?

They are for different function types. -> is always used on an object for static and non-static methods (though I don't think it's good practice use -> for static methods). :: is only used for static methods and can be used on objects (as of PHP 5.3) and more importantly classes.


1 Answers

The PHP Language reference has details on what a function and class is: http://www.php.net/manual/en/langref.php

It also explains most the other features of PHP. If want to learn PHP that is the best place to start.

Functions

The function is a grouping of statements (lines of code).

For example the following statements:

$name = 'mary';
$gender = 'girl';
if ($gender == 'girl') {
  $line = $name . ' had a little pony.';
} else if ($gender == 'boy') {
  $line = $name . ' had a little horse.';
}
echo $line;

Can be grouped together into a function so it can be reused:

getSentence('mary', 'girl');
getSentence('peter', 'boy');
function getSentence($name, $gender) {
  if ($gender == 'girl') {
    $line = $name . ' had a little pony.';
  } else if ($gender == 'boy') {
    $line = $name . ' had a little horse.';
  }
  echo $line;
}

Notice the two function calls:

getSentence('mary', 'girl');
getSentence('peter', 'boy');

These two statements run the whole block of code inside the getSentence function and pass it the variables $name and $gender. With the first function $name = 'mary' and $gender = 'girl' and in the second $name = 'peter' and $gender = 'boy'.

So the main benefit of functions is that you have grouped code for reuse, allowing the passing of different values for the variables needed by the function. These variables are called the function parameters.

Another benefit of having the code grouped is easier readability. You are essentially naming the block of code, and giving them a specific purpose. Making it easy to read and remember it's use.

Another benefit is that redundancy is removed. You do not have to write the block of code more then once. You just define it once, and call it multiple times. This also makes editing of the function code affect all calls to that function - which reduces errors in having to edit multiple locations when changing just one aspect.

eg:

We can make sure the $name string has an uppercase first character.

function getSentence($name, $gender) {
  $name = ucfirst($name);
  if ($gender == 'girl') {
    $line = $name . ' had a little pony.';
  } else if ($gender == 'boy') {
    $line = $name . ' had a little horse.';
  }
  echo $line;
}

We made just one change, and it affected every function call to getSentence(). In this case both:

getSentence('mary', 'girl'); 

and

getSentence('peter', 'boy'); 

Classes are a grouping of functions.

class Play {
  function getSentence($name, $gender) {
    $name = ucfirst($name);
    if ($gender == 'girl') {
      $line = $name . ' had a little pony.';
    } else if ($gender == 'boy') {
      $line = $name . ' had a little horse.';
    }
    echo $line;
  }
  function getSong($name) {
    // code here
  }
}

All we did was put

class Play { /** functions here **/ }

around a group of functions.

This offers the same benefits that functions do for statements except classes does it for functions.

Classes go further to build a programming methodology called Object Oriented programming (OOP), which you can read more about in link to PHP Language reference.

This defines classes as the template or definition of Objects. Objects being similar to real world objects, with the functions being called "methods" that can be called for the object.

So the class Play can be thought of as the object called "Play" with the methods "getSentence" and "getSong". These methods can then manipulate the properties of the object "Play" or return useful information about "Play". In this way, all the code inside Play becomes independent of code elsewhere in the program.

When the code inside Play requires some code elsewhere to function, it can be brought in using "inheritance", which is a major part of OOP. I will not go into detail about this as it is a very broad topic.

I would recommend getting a book on OOP and reading it to really understand why you should use classes and methods and when to use them.

like image 195
Deepu Avatar answered Nov 15 '22 03:11

Deepu