The Street Way

Classes Use In php

Classes :


The Blueprints of A breeds are known as classes they define the basic architecture of the objects available in our programs. Each class is defined as having a set of methods and properties, and you can inherit one class from another-our Breed classes, for example, inherited from the A class, thereby getting all the A methods and properties available. Inheriting is often referred to as subclassingB would be a subclass of A.

    PHP allows you to inherit from precisely one parent class, and you can inherit as many times as you want. For example, the A class could inherit from the class Carnivore, which would contain Cat, A, Bear, etc. Carnivore could inherit from Mammalia, holding all mammals, which could in turn inherit from Vertebrata, holding all  with a backbone, etc.the higher up you go, the more vague the classes become. This is because each class inherits methods and properties from its parent class, as well as adding its own.

NOTE : People often use the terms parent, child, grandparent, etc. to define their class structure. A child class   is one that inherits from another B is a child of A, and would be a grandchild of Carnivore. Carnivore would be the parent of A and grandparent of B-this will make more sense later, when you are creating your own classes and sub-classing freely.

Defining a Class :


    Given the class structure of As and breeds discussed above, it is time to take a look at how that translates into PHP code. Here is the PHP code necessary to define a very basic A class:
                   
               Ex. :-     class A
                    {
                        public function say( )
                        {
                        print "Hello!\n"; // Here you can use 'echo' or 'print' for display message .
                        }
                    }

    Here the A class has just one method, say( ), which outputs "Woof!". Don't worry about the public part for now that just means "can be called by anyone" and we'll be looking at that later. If we create an object of type A, we could call its say( ) method to have it output the message.

NOTE : Class naming conventions follow the same rules as variable naming, excluding the dollar sign at the beginning. You can use any name for your methods, except stdClass and _ _PHP_Incomplete_Class both of these are reserved by PHP.

  • start or end local properties with a special character, so that you are always clear about what variable      is being set. The most common method is to start local properties with an underscore, e.g., _Name, _Age, etc.

  • To follow OOP guidelines strictly, nearly all of your properties should be either private or protected they should not be accessible from outside of an object. More on this later.

  • Write accessors methods to set and get private properties. These methods should be how you interface with the object. To get a property called _Age, write a method Age( ). To set a property called _Age, write a method SetAge( ).

  • Always put properties and methods as low in your inheritance as they can go without repetition. If you find one object has properties and methods it is not supposed to have, you have gone wrong somewhere. For example, while dolphins can swim, gorillas cannot, so do not put a swim( ) method into a Mammal class just to save time.

If you are wondering why it is that accessors methods should be used to read and write properties, it is because OOP practice dictates that objects should be self-contained. That is, other parts of your program should be able to work with them using simple method calls, so that they do not need implicit knowledge of an object's internal structures and operations.

Basic Inheritance :


To extend the A class to breeds, the extends keyword is needed, like this:

        Ex. :- class A {
            public function say( ) {
                    print "Hello !\n";
            }
    }

          class B extends A {
            // Here nothing new
    }



Overriding Method :

PHP allows us to redefine methods in subclasses, which means we can make the B class have its own version of say( ). This is done by redefining the method inside the child class, making the B class look like this:
Ex. :- class B extends A {
            public function say( ) {
                    print "Yap!\n";
            }
    }

   
    We'll come back to inheritance after we look at objects actual instances of our classes.

The Scope Resolution Operator :

 The scope resolution operator is ::two colons next to each other. It is used in object-oriented programming when you want to access static or overridden methods of a class. For example, if you have a method sayhello( ) as well as a sayhello( ) method of a Person object, you would use Person::sayhello( )you resolve which sayhello( ) you mean by using the class name and the scope resolution operator.
   
    The most common use for scope resolution is with the pseudo-class parent. For example, if you want a child object to call its parent's _construct( ) method, you would use parent::_construct( ). We will discuss about  parent::_construct( ) later.

0 comments:

Post a Comment