Programming Basics

  Home arrow Programming Basics arrow Object-Oriented PHP: Fields, Propertie...
PROGRAMMING BASICS

Object-Oriented PHP: Fields, Properties and Methods
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2010-05-12

    Table of Contents:
  • Object-Oriented PHP: Fields, Properties and Methods
  • Properties
  • Methods
  • Method Scopes

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Object-Oriented PHP: Fields, Properties and Methods


    (Page 1 of 4 )

    In this second part of a three-part series that introduces you to PHP and the key concepts of object-oriented programming (OOP), you will learn about field scopes and their classes, properties, and methods. This article is excerpted from chapter six of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).

    Field Scopes

    PHP supports five class field scopes: public, private, protected, final, and static. The first four are introduced in this section, and the static scope is introduced in the later section, “Static Class Members.”

    Public

    You can declare fields in the public scope by prefacing the field with the keyword public. An example follows:

    public . An example follows:You can declare fields in the public scope by prefacing the field with the keyword public. An example follows:

    class Staff
    {
       
    public $name;
      
    /* Other field and method declarations follow... */
    }

    Public fields can then be manipulated and accessed directly by a corresponding object, like so:

    $employee = new Staff();
    $employee->name = "Mary Swanson";
    $name = $employee->name;
    echo "New staff member: $name";

    Not surprisingly, executing this code produces:

    New staff member: Mary Swanson

    Although this might seem like a logical means for maintaining class fields, public fields are actually generally considered taboo to OOP, and for good reason. The reason for shunning such an implementation is that such direct access robs the class of a convenient means for enforcing any sort of data validation. For example, nothing would prevent the user from assigningnamelike so:

    $employee->name = "12345";

    This is certainly not the kind of input you were expecting. To prevent such mishaps from occurring, two solutions are available. One solution involves encapsulating the data within the object, making it available only via a series of interfaces, known as public methods. Data encapsulated in this way is said to be private in scope. The second recommended solution involves the use of properties, and is actually quite similar to the first solution, although it is a tad more convenient in most cases. Private scoping is introduced next, whereas properties are discussed in the later section, “Properties.”

    Private

    Private fields are only accessible from within the class in which they are defined. An example follows:

    class Staff
    {
       
    private $name;
       
    private $telephone;
    }

    Fields designated as private are not directly accessible by an instantiated object, nor are they available to subclasses. If you want to make these fields available to subclasses, consider using the protected scope instead, introduced next. Instead, private fields must be accessed via publicly exposed interfaces, which satisfies one of OOP’s main tenets introduced at the beginning of this chapter: encapsulation. Consider the following example, in which a private field is manipulated by a public method:

    <?php
      
    class Staff
      
    {
         
    private $name;
         
    public function setName($name) {
            
    $this->name = $name;
         
    }
       }
      
    $staff = new Staff;
       $staff->setName("Mary");
    ?>

    Encapsulating the management of such fields within a method enables the developer to maintain tight control over how that field is set. For example, you could add to thesetName()method’s capabilities, to validate that the name is set to solely alphabetical characters and to ensure that it isn’t blank. This strategy is much more reliable than leaving it to the end user to provide valid information.

    Protected

    Just like functions often require variables intended for use only within the function, classes can include fields used for solely internal purposes. Such fields are deemed protected, and are prefaced accordingly. An example follows:

    class Staff
    {
       
    protected $wage;
    }

    Protected fields are also made available to inherited classes for access and manipulation, a trait not shared by private fields. Any attempt by an object to access a protected field will result in a fatal error. Therefore, if you plan on extending the class, you should use protected fields in lieu of private fields.

    Final

    Marking a field as final prevents it from being overridden by a subclass, a matter discussed in further detail in the next chapter. A finalized field is declared like so:

    class Staff
    {
       final $ssn;
      
    ...
    }

    You can also declare methods as final; the procedure for doing so is described in the later section, “Methods.”

    More Programming Basics Articles
    More By Apress Publishing

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP
    - Calendar Construction with PHP
    - PHP`s Calendar Package
    - Getting Modified Versions and Correct Dates ...
    - Combining Date Functions in PHP
    - Using PHP for Date and Time in Programming
    - More Exception Handling with PHP
    - Exception Handling in PHP
    - Error Logging and Handling Exceptions
    - Configuration Directives for Error and Excep...
    - Error and Exception Handling
    - Python Modules for Games


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap