Support

Need help with a tutorial? You may post in the tutorial topic itself so if other people are having the same problem they can find it quickly and easily!
All tutorials are copyright © by Faintmedia.com. You may not copy these tutorials.
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Using Classes Learn the basics of classes Rate Topic: -----

#1 User is offline   Riley Wiebe 

  • Faintmedia CTO
  • Icon
  • View blog
  • Group: Admin
  • Posts: 209
  • Joined: 06-May 08
  • Gender:Male
  • Location:Canada
  • Creator of Faintmedia Raive Studios/Decade Creations Member Northern Storms Admin Creator of Led Salad Coffee Addict Most Posts Purchased 1 - 3 products Reported a bug

Posted 18 November 2008 - 06:57 PM

Once you get further into PHP you will find classes and functions are very nice things to have because it allows you to call a variety of functions at any time. It also reduces the size of the code on one page because the majority of the code will be in the class.

In order to create a class you simply do the following:

<?php
 class name
 {
 }
 ?>


"class" tells PHP that you are creating a class and then the text after it, in this case "name", tells the classes name. Then it is followed by {} anything inside the curly braces is apart of the name class.

You can define variables inside of your class that can be used for any function inside your class. This is helpful if you are creating something like a template system and want several functions to do changes to one source code.

<?php
  class name
  {
     var $example = '';
  }
  ?>


Now you can call the $example variable in any function.



To create a function you need to tell PHP it is a function, give it a name and define any variables the function may need.

<?php
   class name
   {
      var $example = '';
     
     function function_example($var)
     {
     }
   }
   ?>


As you can see we started with function which means it is a function then we made a name for it. For this tutorial we just called it function_example then we have a variable called $var. You do not need a variable I just used one to show you how it worked. If you didn't have a variable it would just be:

<?php
    class name
    {
       var $example = '';
      
      function function_example()
      {
      }
    }
    ?>


But for our example we will use a variable.

Now following the variable we have more curly braces ({}) and anything inside them is apart of the function.

Now we will use that variable to define our $example variable.

In order to call any global variable or function inside the class we use $this->NAME. This means that it is inside of our class.

<?php
    class name
    {
       var $example = '';
      
      function function_example($var)
      {
         $this->example = $var;
      }
    }
    ?>


We have now said that $example will be what $var is.

Now lets create another function which will display this data.

<?php
     class name
     {
        var $example = '';
       
       function function_example($var)
       {
          $this->example = $var;
       }
     
     function display()
     {
         return $this->example;
     }
     }
     ?>


As you can see there is now a new function called display inside that we use return. Return is used to send data to whatever was calling that function in this case we wanted to send the $example variable back.



This is nice and all but now how do we actually use this class and its functions?

<?php
      class name
      {
         var $example = '';
        
        function function_example($var)
        {
           $this->example = $var;
        }
      
      function display()
      {
          return $this->example;
      }
      }
 
 $name = new name;
      ?>


Outside of the class we created a new variable called $name and it equals new name. We use new to assign a class to that variable our class was called name.

Now we are able to use the functions.

<?php
       class name
       {
          var $example = '';
         
         function function_example($var)
         {
            $this->example = $var;
         }
       
       function display()
       {
           return $this->example;
       }
       }
  
  $name = new name;
 $name->function_example('This is an example');
 $name->display();
       ?>


Now we can call our functions we do this by first entering our variable of the class, we used $name. We then follow it by -> and the class name. When you run this nothing will happen because its returned the $example variable but has nothing to do with it.

But, if we echo it we can now see what we entered.

<?php
        class name
        {
           var $example = '';
          
          function function_example($var)
          {
             $this->example = $var;
          }
        
        function display()
        {
            return $this->example;
        }
        }
   
   $name = new name;
  $name->function_example('This is an example');
  echo $name->display();
        ?>


That is just some basic class functionality just to give you a few examples to play with.

Like This Tutorial? Join the Community Today!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users