Skip to content

Commit

Permalink
add scope resolution operator
Browse files Browse the repository at this point in the history
  • Loading branch information
neenjaw committed Nov 16, 2022
1 parent dec4828 commit d8cc930
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 320 deletions.
5 changes: 5 additions & 0 deletions concepts/scope-resolution-operator/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"blurb": "The Scope Resolution Modifier (`::`)",
"authors": ["neenjaw"],
"contributors": []
}
99 changes: 99 additions & 0 deletions concepts/scope-resolution-operator/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Scope Resolution Operator

The scope resolution operator (`::`) allows us to reference a class to access its name constants, static properties, or static methods.

```php
class MyClass
{
public const MY_VALUE = 1;
}

// MyClass::class => "MyClass"
// MyClass::MY_VALUE => 1
```

## Using `self` and `static` with the Scope Resolution Operator

It may be required for an object to reference its own scope. To facilitate this, the `self` keyword allows a class to reference its own scope. Using the `self` keyword decouples the class from its own name -- making it easier to maintain code over time.

```php
class MyClass
{
public const MY_VALUE = 1;

public function getMyValue(): int
{
return self::MY_VALUE;
}
}

// (new MyClass)->getMyValue() => 1
```

However, `self` is lexicographically bound to the class it appears in.

```php
class MyOtherClass extends MyClass
{
public const MY_VALUE = 99;
}

// (new MyOtherClass)->getMyValue() => 1
```

Evaluating `MyOtherClass::getMyValue` still results in `1` rather than `99`. To work around this, use the `static` keyword with the scope resolution operator to "late bind" the reference to the current descendent class.

```php
// Update MyClass:
class MyClass
{
public const MY_VALUE = 1;

public function getMyValue(): int
{
return static::MY_VALUE;
}
}
```

## Using `parent` with the Scope Resolution Operator

There may be a need to specifically reference a constant, property or method available from the parent scope:

```php
class MyClass
{
public function getMaximum(): int
{
return 5;
}
}

class AnotherClass extends MyClass
{
public function getMaximum(): int
{
return parent::getMaximum() * 2;
}
}

// (new AnotherClass())->getMaximum() => 10
```

Most commonly in object oriented programming `parent` is used to call the call the parent constructor when an object is being instantiated:

```php
class ChildClass extends ParentClass
{
public function __construct(): void
{
parent::__construct();
}
}
```

## Caveats

### Constant, Property, and Method Visibility

The scope resolution operator still follows the rules set out by `public`, `private`, and `protected` visibility modifiers.
66 changes: 66 additions & 0 deletions concepts/scope-resolution-operator/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Scope Resolution Operator

The scope resolution operator (`::`) allows us to reference a class to access its name constants, static properties, or static methods.

```php
class MyClass
{
public const MY_VALUE = 1;
}

// MyClass::class => "MyClass"
// MyClass::MY_VALUE => 1
```

## Using `self` and `static` with the Scope Resolution Operator

It may be required for an object to reference its own scope. To facilitate this, the `self` keyword allows a class to reference its own scope. Using the `self` keyword decouples the class from its own name -- making it easier to maintain code over time.

```php
class MyClass
{
public const MY_VALUE = 1;

public function getMyValue(): int
{
return self::MY_VALUE;
}
}

// (new MyClass)->getMyValue() => 1
```

Use the `static` keyword with the scope resolution operator to "late bind" the reference to the current descendent class.

```php
// Update MyClass:
class MyClass
{
public const MY_VALUE = 1;

public function getMyValue(): int
{
return static::MY_VALUE;
}
}
```

## Using `parent` with the Scope Resolution Operator

The `parent` keyword is used to reference parent class constant, properties or methods, Commonly it is used to call the call the parent constructor when an object is being instantiated:

```php
class ChildClass extends ParentClass
{
public function __construct(): void
{
parent::__construct();
}
}
```

## Caveats

### Constant, Property, and Method Visibility

The scope resolution operator still follows the rules set out by `public`, `private`, and `protected` visibility modifiers.
6 changes: 6 additions & 0 deletions concepts/scope-resolution-operator/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"url": "https://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php",
"description": "Scope Resolution Operator (`::`)"
}
]
Loading

0 comments on commit d8cc930

Please sign in to comment.