Protected static methods php

Static methods in PHP OOP

In this tutorial we will learn more about the static methods in PHP OOP. We will discuss what static methods are meant for, when to use them, the syntax and the difference between static and non-static methods.

What are static methods in PHP?

  • Static methods are created using static keyword.
  • Static methods of a class are called directly. It means, we don’t need to create the instance of the class to call its static method.
  • A class can contains both static and non-static methods.
  • To call the static method of a class, we use scope resolution operator ::

Syntax of Static Method in PHP

Syntax to call the static method

Example

 > // Calling static method Example::greetings(); ?>
  • In the above example, we create a class that contains static method in it.
  • We call the static method of this class outside using scope resolution method.

Calling static method inside the class – PHP

  • In PHP, self keyword allows to call its own static method inside the class. Look at the following example in which we create a class with static method and call it inside the constructor of the class.
  • self::methodName() is the general syntax to call static method inside the class.
 public function __construct() < self::greetings(); >> new Example(); ?>

Calling Static Method inside another class – PHP

It’s very interesting to use case of static methods, that we can call a public static method inside another class. Remember, it should be a public static method.

Читайте также:  Php not allowed exception

Calling a static method in child class – PHP

In the case of inheritance, we can call the static method of the parent class inside the child class using the parent keyword. Look at the example below.

 > class Child1 extends domain < public $name; public function __construct() < $this->name= parent::getMessage(); > > $child1= new child1(); echo $child1-> name; ?>
  • In the above example, we create a parent class with some static method.
  • Also, we create another class that extends the parent class.
  • Call the static method of the parent class using parent keyword followed by the name of the static method.

Note: As we mentioned in the previous section that calling a static method inside another class required it to be public. However, in the case of calling the static method of the parent class (inheritance), the static method access modifier can be public or protected.

Difference between Static and Non Static Methods

Static Method Non Static Method
We can access only static properties of the class or of another class inside the static method. We can access both the static and non-static properties of the class or of another class inside the non-static method.
Static Method uses early binding or compile-time binding. The non-Static method uses dynamic or runtime binding.
Static methods maintain the state during the execution of the program, so due to early binding, they cannot be overridden by another class. Due to the runtime binding or dynamic behavior of the non-static method, we can override them in another class.
Memory allocation in the static method is more efficient because it specifies a memory block inside the ram once the program is executed. It never does it again until the program terminates. Non-static methods are less efficient in memory allocation because of dynamic binding. Every time the non-static method is called, it allocates memory and deallocates after the execution of the method completes.
Читайте также:  Php заменить несколько пробелов одним

Static vs Non-Static Method

Источник

Protected static methods php

INSIDE CODE and OUTSIDE CODE

class Item
/**
* This is INSIDE CODE because it is written INSIDE the class.
*/
public $label ;
public $price ;
>

/**
* This is OUTSIDE CODE because it is written OUTSIDE the class.
*/
$item = new Item ();
$item -> label = ‘Ink-Jet Tatoo Gun’ ;
$item -> price = 49.99 ;

?>

Ok, that’s simple enough. I got it inside and out. The big problem with this is that the Item class is COMPLETELY IGNORANT in the following ways:
* It REQUIRES OUTSIDE CODE to do all the work AND to know what and how to do it — huge mistake.
* OUTSIDE CODE can cast Item properties to any other PHP types (booleans, integers, floats, strings, arrays, and objects etc.) — another huge mistake.

Note: we did it correctly above, but what if someone made an array for $price? FYI: PHP has no clue what we mean by an Item, especially by the terms of our class definition above. To PHP, our Item is something with two properties (mutable in every way) and that’s it. As far as PHP is concerned, we can pack the entire set of Britannica Encyclopedias into the price slot. When that happens, we no longer have what we expect an Item to be.

INSIDE CODE should keep the integrity of the object. For example, our class definition should keep $label a string and $price a float — which means only strings can come IN and OUT of the class for label, and only floats can come IN and OUT of the class for price.

class Item
/**
* Here’s the new INSIDE CODE and the Rules to follow:
*
* 1. STOP ACCESS to properties via $item->label and $item->price,
* by using the protected keyword.
* 2. FORCE the use of public functions.
* 3. ONLY strings are allowed IN & OUT of this class for $label
* via the getLabel and setLabel functions.
* 4. ONLY floats are allowed IN & OUT of this class for $price
* via the getPrice and setPrice functions.
*/

protected $label = ‘Unknown Item’ ; // Rule 1 — protected.
protected $price = 0.0 ; // Rule 1 — protected.

public function getLabel () < // Rule 2 - public function.
return $this -> label ; // Rule 3 — string OUT for $label.
>

public function getPrice () < // Rule 2 - public function.
return $this -> price ; // Rule 4 — float OUT for $price.
>

public function setLabel ( $label ) // Rule 2 — public function.
/**
* Make sure $label is a PHP string that can be used in a SORTING
* alogorithm, NOT a boolean, number, array, or object that can’t
* properly sort — AND to make sure that the getLabel() function
* ALWAYS returns a genuine PHP string.
*
* Using a RegExp would improve this function, however, the main
* point is the one made above.
*/

if( is_string ( $label ))
$this -> label = (string) $label ; // Rule 3 — string IN for $label.
>
>

public function setPrice ( $price ) // Rule 2 — public function.
/**
* Make sure $price is a PHP float so that it can be used in a
* NUMERICAL CALCULATION. Do not accept boolean, string, array or
* some other object that can’t be included in a simple calculation.
* This will ensure that the getPrice() function ALWAYS returns an
* authentic, genuine, full-flavored PHP number and nothing but.
*
* Checking for positive values may improve this function,
* however, the main point is the one made above.
*/

if( is_numeric ( $price ))
$this -> price = (float) $price ; // Rule 4 — float IN for $price.
>
>
>

?>

Now there is nothing OUTSIDE CODE can do to obscure the INSIDES of an Item. In other words, every instance of Item will always look and behave like any other Item complete with a label and a price, AND you can group them together and they will interact without disruption. Even though there is room for improvement, the basics are there, and PHP will not hassle you. which means you can keep your hair!

If you have problems with overriding private methods in extended classes, read this:)

The manual says that «Private limits visibility only to the class that defines the item». That means extended children classes do not see the private methods of parent class and vice versa also.

As a result, parents and children can have different implementations of the «same» private methods, depending on where you call them (e.g. parent or child class instance). Why? Because private methods are visible only for the class that defines them and the child class does not see the parent’s private methods. If the child doesn’t see the parent’s private methods, the child can’t override them. Scopes are different. In other words — each class has a private set of private variables that no-one else has access to.

A sample demonstrating the percularities of private methods when extending classes:

abstract class base <
public function inherited () <
$this -> overridden ();
>
private function overridden () <
echo ‘base’ ;
>
>

class child extends base <
private function overridden () <
echo ‘child’ ;
>
>

$test = new child ();
$test -> inherited ();
?>

Output will be «base».

If you want the inherited methods to use overridden functionality in extended classes but public sounds too loose, use protected. That’s what it is for:)

A sample that works as intended:

abstract class base <
public function inherited () <
$this -> overridden ();
>
protected function overridden () <
echo ‘base’ ;
>
>

class child extends base <
protected function overridden () <
echo ‘child’ ;
>
>

$test = new child ();
$test -> inherited ();
?>
Output will be «child».

Источник

PHP OOP — Static Methods

Static methods can be called directly — without creating an instance of the class first.

Static methods are declared with the static keyword:

Syntax

To access a static method use the class name, double colon (::), and the method name:

Syntax

Example

class greeting public static function welcome() echo «Hello World!»;
>
>

// Call static method
greeting::welcome();
?>

Example Explained

Here, we declare a static method: welcome(). Then, we call the static method by using the class name, double colon (::), and the method name (without creating an instance of the class first).

PHP — More on Static Methods

A class can have both static and non-static methods. A static method can be accessed from a method in the same class using the self keyword and double colon (::):

Example

class greeting public static function welcome() echo «Hello World!»;
>

public function __construct() self::welcome();
>
>

Static methods can also be called from methods in other classes. To do this, the static method should be public :

Example

class A public static function welcome() echo «Hello World!»;
>
>

class B public function message() A::welcome();
>
>

$obj = new B();
echo $obj -> message();
?>

To call a static method from a child class, use the parent keyword inside the child class. Here, the static method can be public or protected .

Example

class domain protected static function getWebsiteName() return «W3Schools.com»;
>
>

class domainW3 extends domain public $websiteName;
public function __construct() $this->websiteName = parent::getWebsiteName();
>
>

$domainW3 = new domainW3;
echo $domainW3 -> websiteName;
?>

Источник

Оцените статью