PHP classes - objects

the object-oriented programming style in php is a great way to build modular, reusable code, letting you create large applications that are relatively easy to maintain.

classes, objects, properties, and methods are the basic building blocks that you can use to create object-oriented applications in php.

a PHP class is a unit of code that describes the characteristics and behaviors of something, or of a group of things.

the syntax to create a class is pretty straightforward: declare a class using the class keyword, followed by the name of the class and a set of curly braces ( {} ).

syntax:

         class      class_name
         {
                          // property declaration part
                                    
                                    
                                    
                          // function definition part                            
         }

note : a valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

a common coding standard is to begin a class name with a capital letter.


PHP  object

PHP object an individual instance of the data structure defined by a class. objects are also knows as instance.

note : the new keyword is used to create an instance of a class.

         $object  =  new  class_name;

or

         $object  =  new  class_name();

note : when assigning an already created instance of a class to a new variable, the new variable will access the same instance as the object that was assigned.

note : -> symbol is used to access class members through class object.

<?php  
            class     car{
            
                 private    $weight = "100kg";
                 
                 private    $color  = "red";            // class properties
                 
                 public     $price  = "$1000";
                 
                 
                            function    show(){   // class method
                            
                              
                                 echo   "<br/>". $this->weight;
                                 
                                 echo   "<br/>". $this->color;
                                 
                                 echo   "<br/>". $this->price;
                                 
                              }
                              
                  }                   

                    $object  =  new   car();  // create a class  object
                    
                     
                    print_r( $object );
                    
                    echo  ( "<br/>".$object->price ); // access class public property
                    
                    // outside the class, you can't access the class private properties 
                    
                    $object->show();  //call the show method 
?>

note : $this variable within show() method is refer to the current object.


php class constructors and destructors

constructor : a php class constructor is a 'special' member function whose task is to initialize the objects of its class.

the constructor is invoked whenever an object of its associated class is created.

in php, _ _construct() method is used to define a constructor within a class.

syntax:

         void    __construct( )

they are invoked automatically when the object are created.

they can't be inherited, though a derived class can call the base class constructor.

destructor : the PHP destructor is used to destroy the objects.

the destructor called during the script shutdown have http headers already sent.

syntax:

         void  __destruct()
 <?php
 
    class    safari{
           
                    public   $safari_weight  = null;
                 
                    public   $safari_price   = null;
                 
                 
                              function   __construct(){  //constructor with no argument
                     
                                                          $this->safari_weight = "200kg";
                           
                                                          $this->safari_price  = "$3000";
                           
                                        echo  "<br/> safari class object is created ";
                           
                                       }
                         
                              function   __destruct(){  // destroy the safari object
                     
                     
                                        echo  "<br/> destroy the object";
                          
                                      }
                                      
                          }            
                       
                              $safari_object   =  new safari();
                        
                              $safari_object1  =  new safari();
                        
                              var_dump( $safari_object );
                        
                              var_dump( $safari_object1 );
 ?>

note : when the end of a file is reached, php automatically releases all resources.