-2

I have a php code that is procedural. So I am changing it to object oriented. I have a function like this:

<?php

 function get_username(){
  $user= "maria";
  return $user;
 }

 function get_name(){
  $user= "Maria Campbell";
  return $user;
 }

echo get_username();
echo get_name();

?>

and if I convert it to object oriented, it will be like:

<?php

class user{

 public function get_username(){
  $user= "maria";
  return $user;
 }

 public function get_name(){
  $user= "Maria Campbell";
  return $user;
 }

}


$userfunctions = new user();

echo $userfunctions->get_username();
echo $userfunctions->get_name();


?>

my question is, what I gain doing the second way? it is the same thing, isnt it?

Gabriela Dias
  • 349
  • 2
  • 12
  • 2
    Actually - you gain nothing, on such simple examples. Object shouldn't be just a bunch of functions wrapped in class. Keep it procedural, if it works? – sinisake Apr 07 '15 at 13:03

1 Answers1

0

The difference at first glace, is nothing. However, when you start using classes, you can use inheritance and other great things, which greatly improves your code and gives you more scope to various techniques, such as;

If you wanted a more in depth answer, using a class has major differences between your procedural code. But, if we take your code literally, there is not enough difference to comment on (apart from 1 being in a class and the other not).

Community
  • 1
  • 1
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49