Found 1046 Articles for PHP

PHP Visibility modes

Malhar Lathkar
Updated on 18-Sep-2020 11:56:56

2K+ Views

IntroductionIn PHP, it is possible to have a user-defined compund data type using class keyword. A new instance of class is an object. Charactersitics of object are as per definition of class, which may contain property, constant and method members.Accessibility (also called visibility) of a class member depends on visibility prefix keyword attached in its definition. PHP has three visibility keywords - public, private and protected. A class member declared with public keyword is accessible from anywhare. A protected member is accessible from within its class and by inheriting class. On the other hand, a private member can only be ... Read More

PHP Late Static Bindings

Malhar Lathkar
Updated on 18-Sep-2020 11:48:26

181 Views

IntroductionThis feature of late static binding in PHP is used to refercalled class in static inheritance. When static methods are called, name of class is attached with scope resolution operator (::) while in case of other instance methods we call them using name of object. The static:: will not be resolved using the class in which method is defined ,instead will be computed using runtime information. Static references to the current class are resolved using the class in which the function belongs, not where it was definedExampleIn following code, parent class calls static ethod with self:: prefix. Same method when ... Read More

PHP Object Serialization

Malhar Lathkar
Updated on 18-Sep-2020 11:38:42

1K+ Views

IntroductionA string representation of any object in the form of byte-stream is obtained by serialze() function in PHP. All property variables of object are contained in the string and methods are not saved. This string can be stored in any file.To retrieve the object from the byte stream, there is unserialize() function. Definition of corresponding class must be available before calling unserialize()function.ExampleFirst let us serialize an object of following class and store the string in a file.In current folder, obj.txt is created. To unserialize, following code reconstructs object from the byte stream read from the given fileExampleRead More

PHP Scope Resolution Operator (::)

Malhar Lathkar
Updated on 18-Sep-2020 11:37:12

6K+ Views

IntroductionIn PHP, the double colon :: is defined as Scope Resolution Operator. It used when when we want to access constants, properties and methods defined at class level. When referring to these items outside class definition, name of class is used along with scope resolution operator. This operator is also called Paamayim Nekudotayim, which in Hebrew means double colon.SyntaxInside classTo access class level items inside any method, keyword - self is usedIn child classIf a parent class method overridden by a child class and you need to call the corresponding parent method, it must be prefixed with parent keyword and scope resolution ... Read More

PHP Class properties

Malhar Lathkar
Updated on 18-Sep-2020 11:34:13

7K+ Views

IntroductionData members declared inside class are called properties. Property is sometimes referred to as attribute or field. In PHP, a property is qualified by one of the access specifier keywords, public, private or protected. Name of property could be any valid label in PHP. Value of property can be different for each instance of class. That's why it is sometimes referred as instance variable.Inside any instance method, property can be accessed by calling object's context available as a pesudo-variable $this. If a property is declared as public, it is available to object with the help of -> operator. If a ... Read More

PHP Objects and references

Malhar Lathkar
Updated on 18-Sep-2020 11:27:25

3K+ Views

IntroductionIn PHP, objects are passed by references by default. Here, reference is an alias, which allows two different variables to write to the same value. An object variable doesn't contain the object itself as value. It only contains an object identifier which allows using which the actual object is found. When an object is sent by argument, returned or assigned, the different variables are not aliases − instead, they hold a copy of the identifier, pointing to the same object.ExamplePHP has spl_object_hash() function that returns unique hash ID of an object. In following code, two object variables, referring to same ... Read More

PHP Comparing Objects

Malhar Lathkar
Updated on 18-Sep-2020 11:25:02

3K+ Views

IntroductionPHP has a comparison operator == using which a simple comarison of two objecs variables can be performed. It returns true if both belong to same class and values of corresponding properties are are same.PHP's === operator compares two object variables and returns true if and only if they refer to same instance of same classWe use following two classes for comparison of objects with these opratorsExampletwo objects of same classExample$a=new test1(10, 20); $b=new test1(10, 20); echo "two objects of same class"; echo "using == operator : "; var_dump($a==$b); echo "using === operator : "; var_dump($a===$b);Outputtwo objects of same class using == operator ... Read More

PHP Magic Methods

Malhar Lathkar
Updated on 18-Sep-2020 11:23:16

8K+ Views

IntroductionMagic methods in PHP are special methods that are aimed to perform certain tasks. These methods are named with double underscore (__) as prefix. All these function names are reserved and can't be used for any purpose other than associated magical functionality. Magical method in a class must be declared public. These methods act as interceptors that are automatically called when certain conditions are met.Following magical methods are currently available in PHP__sleeppublic __sleep ( void ) : arrayserialize() method in class checks if it has a function name __sleep(). If so, that function is executed prior to any serialization. It ... Read More

PHP Object Interfaces

Malhar Lathkar
Updated on 18-Sep-2020 11:15:49

536 Views

IntroductionInterface is an important feature of object oriented programming by which it is possible to specify methods to be implemented by a class, without having to define how they should be implemented.PHP supports interface by way if interface keyword. Interface is similar to class but with methods without definition body. Methods in interface must be public. An inherited class that implements these methods must be defined with implements keyword instead of extends keyword, and must provide implementations of all methods in parent interface.SyntaxAll methods from interface must be defined by the implementing class, otherwise PHP parser throws exceptionExample Live DemoOutputThe error ... Read More

PHP Object Inheritance

Malhar Lathkar
Updated on 18-Sep-2020 11:12:10

3K+ Views

IntroductionInheritance is an important principle of object oriented programming methodology. Using this principle, relation between two classes can be defined. PHP supports inheritance in its object model.PHP uses extends keyword to establish relationship between two classes.Syntaxclass B extends Awhere A is the base class (also called parent called) and B is called a subclass or child class. Child class inherits public and protected methods of parent class. Child class may redefine or override any of inherited methods. If not, inherited methods will retain their functionality as defined in parent class, when used with object of child class.Definition of parent class ... Read More

Advertisements