PHP Reference Function in a Class

PHP Reference Function in a Class thumbnail
PHP scripting is involved in lots of website types such as customer service.

When developers build sites in the PHP scripting language, they can use object oriented development. In an object oriented application, a group of code objects work together to deliver the required functionality. Programmers write class declarations in which they dictate the functions that objects of a class will provide. By creating objects of a class, an application can therefore reference these functions. Object oriented development allows programmers to focus on specific areas of functionality.

  1. Object Oriented Development

    • Object oriented development is featured in many different types of applications, including websites. In PHP, a team of programmers can work on a single site, with each focusing on a particular area of processing. The idea in object oriented development is that programmers working on code components should be able to make use of other components without having to understand their internal details. Being able to call on functions within a class from code that is external to it is a key element in this model. Programmers can create objects with specific responsibilities and behavior, utilizing these behaviors by referring to the functions in the object's class.

    Class Declarations

    • In PHP, a class declaration outlines the name, variables and functions for a set of application objects. The following sample code demonstrates a class declaration outline:

      <?php
      class Customer {
      var $name;
      function Customer($c_name=""){
      $this->$name=$c_name;
      }
      /*class functions*/
      }
      ?>

      This class declaration defines Customer objects. The class has a variable representing the customer name, which is set by the constructor method. The constructor method has the same name as the class, taking a string parameter. External code can create objects of the class as follows:
      $my_cust = new Customer("Mary");

      When this code executes, the constructor method in the class executes, setting the name variable with the passed value. If the code creating the object does not supply a string parameter, the name variable is set by default to an empty string. After the constructor method, the class declaration can list one or more functions.

    Function Outlines

    • Functions in PHP classes typically look the same as functions in any other PHP scripts. They provide a name and parameters, optionally returning a value. The following sample code demonstrates a class function:

      function get_greeting($term) {
      return "<p>".$term." ".$this->$name."</p>";
      }

      The code takes a passed string an concatenates it with the Customer object name. The return string is formatted as HTML for display within a user's Web browser. The function could alternatively take multiple parameters of different types. Class functions do not need to return anything, this is optional.

    Calling Functions

    • To call a class function, PHP code must create an object of the class, then refer to it by name, followed by the function name. The following extended sample code demonstrates the technique:

      $my_cust = new Customer("Mary");
      echo $my_cust->get_greeting("Hello");

      This will cause the script to output the following HTML code:

      <p>Hello Mary</p>

      The class therefore uses functions to tailor the output of the site to a particular user, with this user modeled within the application as a Customer object. Most class functions can only be referenced using object instances in this way.

Related Searches:

References

Resources

  • Photo Credit Hemera Technologies/Photos.com/Getty Images

Comments

Related Ads

Featured