Udjuni.com

udjuni.com search:




PHP Objects and classes


Object oriented programming is defined to be a programming paradigm that uses objects or data structures consisting of datafields and methods. This paradigm is also composed of interactions among datafields and methods. The programming techniques may include features such as information hiding, data abstraction, encapsulation, modularity, polymorphism, and inheritance.

How are objects created in PHP?


To create an object, you will first have to define a class of the object, which is considered to be a blueprint or prototype from which objects are created. For example, you could define a class called mamals and from that class you could create objects that share the characteristics of mamals. These objects could be humans, cattle and so on. Here is how you define a class and it its objects in PHP, as a matter of fact most object-oriented programming languages use the same or similar syntax:
<php 
class mamal{
// class code in here;
}
$object = new mamal();
echo "This is the object type you jusy created".gettype($object);
?>


Note here that the object has to be defined outside the class definition. It could be within the same file or in a different file.

Properties and methods of objects


Using our example of a mamal, we could say that all mamals have legs, backbone, lungs and breathe in air. Those are characteristics of all mamals, but each species of the class mamal could have different characteristics. For example, we could say that humans run, but horses can also gallop which is a characteristic or property specifically associated with horses. Here is how you define object properties:
<php
class mamal{
  var $movement;
  var $mamaltype;
  
  function mamal_movement($mamaltype){
   if($this->$mamaltype == 'Human'){
     $this->$movement = "run"
     return $this->$movement;
   }else{
     $this->$movement = "Gallop"
     return $this->$movement;
   }
  
  }
$mamaltype = "human";
$object = new mamal();
$object->mamal_movement($mamaltype); 
?>


The code above defines a class mamal, within that class we defined two class object properties ($mamaltype and $movement). A method of an object is nothing more than a function defined within a class. Notice how we defined an object outside of that class and called a method from the class. That method is passed a value in the peoperty $mamaltype, depending on what that value is, the class method will return an appropriate value. In our case, because we have assigned the value "human" to mamal type, the class method will return the value "run".

RELATED ARTICLES:


Introduction to Programming
What exactly is programming? Programming can be defined as the art of telling a computer to do whatever you want it to. If you are reading this posting you probably started off with switching the computer on and then opened the browser that got you to this page. Every comp

Introduction to Java programming
Computer program as we learned in the posting Introduction to Programming

Did not find it? Try udjuni.com search:







© 2009 UdjunI LLC. All rights reserved | Privacy policy | Tutorial Index | RSS Feed