Object Operators in PHP

Answer

There are two object operators in PHP. The symbols are -> and ::

-> Operator

The object operator -> is used when you have to access an Instance property or call a method just like a dot (.) in other languages. For example, if there is a ‘Students’ class that includes the method ‘Marks’, we access it by creating an object say ‘Student’. In other languages:

Student.Marks();

In PHP, The object operator -> is used for this purpose e.g.

$Student->Marks();

:: or scope resolution operator

:: or scope resolution operator is used when you want to call a static method, variable or a parent class’s version of a method within a child class. It is called scope resolution operator as it is used to refer the scope of some program context or a block e.g. classes, objects, namespace etc.

For example,

class Students
{ 
function getMarks()
 { 
   return $marks;
 }  
} 
$marks = Students::getMarks();

:: operator is used when the object has not been created until the class functions or variables are accessed outside the scope of the class.


2 thoughts on “Object Operators in PHP

Add yours

  1. I see you don’t monetize code.blog, don’t waste your traffic, you can earn additional bucks every month with new
    monetization method. This is the best adsense alternative for any type of website (they approve all sites), for more
    details simply search in gooogle: murgrabia’s tools

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

Create your website at WordPress.com
Get started
%d bloggers like this: