PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Working with Objects in PHP 5

Working with Objects in PHP 5

Browse Pages: 2  > >>

Working with Objects in PHP 5In this paper, we will cover the following topics:

  • What objects and classes are
  • How to create classes and instantiate objects
  • How to create and access properties and methods
  • How to manage access to properties and methods
  • How to create classes that inherit functionality from others
  • How to find out about objects in your code
  • How to save objects to a string that can be stored in a file or database

Object-oriented programming can be considered by some to be inherently "dangerous". It can change the way you think about coding and how you approach each project, and once the concepts have a hold on you, they don't let go. PHP, like Perl before it, has progressively incorporated more object-oriented aspects into its syntax and structure. PHP 4 made it possible to use object-oriented code at the heart of a project. PHP 5 has extended PHP's object-oriented features still further.

Before you continue, please make sure that you have a basic understanding of the PHP language and it's syntax. You can learn more by reading the official PHP documentation.

So, What Exactly is an Object?

An object is an enclosed bundle of variables and functions forged from a special template called a class. Objects hide a lot of their inner workings away from the code that uses them, providing instead easy interfaces through which you can send them orders and they can return information. These interfaces are special functions called methods. All the methods of an object have access to special variables called properties.

By defining a class, you lay down a set of characteristics. By creating objects of that type, you create entities that share these characteristics but might initialize them as different values. You might create an automobile class, for example. This class would have a color characteristic. All automobile objects would share the characteristic of color , but some would initialize it to "blue," others to "green," and so on.

Perhaps the greatest benefit of object-oriented code is its reusability. Because the classes used to create objects are self-enclosed, they can be easily pulled from one project and used in another. Additionally, it is possible to create child classes that inherit and override the characteristics of their parents. This technique can allow you to create progressively more complex and specialized objects that can draw on base functionality while adding more of their own.

Perhaps the best way to explain object-oriented programming is to do it.

Creating Your First PHP Object

To create an object, you must first design the blueprint, or the "template" from which it can be created. This template is known as a class, and in PHP, you must declare it with the class keyword:

class Item {

// a very minimal class

}

The Item class is the basis from which you can instantiate any number of Item objects. To create an instance of an object, you must use the new statement:

$obj1 = new Item();
$obj2 = new Item();
print "$obj1 is an ".gettype($obj1)."<br />";
print "$obj2 is an ".gettype($obj2)."<br />";

You can test that $obj1 and $obj2 contain objects with PHP's gettype() function. gettype() accept any variable and returns a string that should tell you what you are dealing with. In a loosely typed language like PHP, gettype() is useful when checking arguments sent to functions. In the previous example, gettype() returns the string "object", which is then displayed in the browser.

You have now confirmed that you have created two objects. Of course, they're not useful to you yet, but they help to make an important point. You can think of a class as a mold from which you can create as many objects as you want. Now we'll add a few more features to your class to make the objects a bit more interesting, functional, and inherently useful.



Author's URL: X2 Studios
Browse Pages: 2  > >>
PHP is open source scripting language. It\'s widely used to develop web applications. More PHP Tutorials: Featured Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org


Like us to: