Jump to content

OOP Example


nmts

Recommended Posts

Index.php

       <?php
           $stefan = new person("Stefan Mischook");
           echo "Stefan's full name: ".$stefan->get_name();
       ?>    

 

 

class_lib.php

<?php

   class person {
       var $name;
       function __construct($persons_name) {        
           $this->name = $persons_name;        
       }        

       function set_name($new_name) {
             $this->name = $new_name;
       }    

       function get_name() {        
             return $this->name;        
        }        

   }

?>

 

Why even have var $name; if the code works fine without it? With the construct, would there be a need for the set_name function?

Link to comment
Share on other sites

I'm not sure I can answer your first question... I know that PHP doesn't necessarily require you to declare your variable names before using them. I imagine that it is best practice to declare your class variables at the top of the class -- makes them easier to keep track of. Someone else who's a bit more familiar with the technicalities of PHP might be better at answering this.

 

In regards to your second question, it's usually best practice to make variables within the class inaccessible to code outside the class. Thus, getter and setter functions are used to get and set variables. Yes, since the name variable is set in the constructor, perhaps you don't technically need a set_name function -- at least at first. But, for example, what if your person is female, and whose name changes after they get married? Are you going to make a new Person object just for that name change (and lose any other data you had stored about them?) Being able to get and set that data could be very important.

Link to comment
Share on other sites

One example to think about regarding your first question... Take a look at this code:

 

   class person {
       var $var2 = '';

       function __construct($persons_name) {        
           $this->name = $persons_name;        
       }        

       function get_var2() {        
             return $this->var2;        
        }        

   }

    $stefan = new person("Stefan Mischook");
   echo $stefan->get_var2();
?>

 

What if the $var2 variable isn't set in the constructor, and then you try to use the get_var2() function? Without this line "var $var2 = '';" at the start of your class you'll get a "Undefined property: person::$var2" error that will stop the script from working. That seems like a pretty good reason to declare your class variables at the start of the class.

Link to comment
Share on other sites

Index.php

           $stefan = new person("Stefan Mischook");
           echo "Stefan's full name: ".$stefan->get_name();
       ?>    

 

 

class_lib.php


   class person {
       var $name;
       function __construct($persons_name) {        
           $this->name = $persons_name;        
       }        

       function set_name($new_name) {
             $this->name = $new_name;
       }    

       function get_name() {        
             return $this->name;        
        }        

   }

?>

 

Why even have var $name; if the code works fine without it? With the construct, would there be a need for the set_name function?

 

well that example is a poor one, you should not use the var declaration, instead go with public/private/protected. Using var can give you a false feeling that the content is safe but var is publicly available. The above example would make sense if the variable $stefan was private or protected, as this would mean that the variable could only be used by functions with in the class with the exception of static ones.

 

this means that the variable $name within that person class can be accessed and changed at will by anyone with access:

 

$test = new person('jimmy');

echo $test->name;
echo '
';
$test->name = 'wanker';
echo $test->name;

 

now the above code should look something like this instead:

 

   final class person {
       private $name;
       function __construct($persons_name) {        
           $this->name = $persons_name;        
       }        

       function set_name($new_name) {
             $this->name = $new_name;
       }    

       function get_name() {        
             return $this->name;        
        }        

   }
?>

 

This follows good coding practises, it is coded in a better secure-code way.

Here having functions that return the name makes sense as this is the only way we can access the data inside the name variable because it's private.

 

So remember to ditch using var, as this is a quite shady way of doing it, use private/protected/public. This makes the code a lot easier to read and understand.

 

Also we use the keyword final before our class, this is standard practise when coding OOP in PHP and if the class you are coding will not be inherited by other classes you are supposed to put final there, this is for several reasons: security, code performs better (optimization), code is easy to read. As you automatically see what is globaly available and what's not and so on.

 

you can also use final on functions that still are available to be used by child classes but where you disable the possibility to override or change those functions.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...