Usage Analysis of PHP Object Oriented Type Constraints

  • 2021-12-12 03:54:52
  • OfStack

This article illustrates the PHP object-oriented type constraints. Share it for your reference, as follows:

What is a type constraint

It is required that a variable can only use (accept and store) a specified data type; php is a "weakly typed language" and generally does not support type constraints; Correspondingly, strongly typed languages and type constraints are their "basic characteristics".

In php, only local partial type constraints are supported

In php, it is only supported to set the constraint target of type on the formal parameter of function (or method), in the following form:


function  Method name ( [ Types required to be used ] $p1, [ Types required to be used ] $p2, ......){
  //....
}

Description:

When defining a function (method), a formal parameter can be used with or without type constraints; If a type constraint is used, the corresponding real argument must be of the required type; Type constraints that can be used are only available in the following situations:

① Array: array

Object: Use the name of the class, and the passed arguments must be instances of the class

③ Interface: Use the name of the interface, and the passed arguments must be instances of the class that implements the interface


<?php
// Demonstrate type constraints 
interface USB{} // Interface 
class A{}  // Class 
class B implements USB{}  // Realized USB Class of interface 
function f1($p1, array $p2, A $p3, USB $P4){
  echo "<br /> Unconstrained p1 : " . $p1;
  echo "<br /> The requirement is an array p2 : " ;
    print_r($p2);
  echo "<br /> Requirements are classes A Object of: ";
    var_dump($p3);
  echo "<br /> The requirement is that the implementation is realized USB Interface: ";
    var_dump($P4);
}
$obj1 = new A();
$obj2 = new B();
$arr = array();
// Demonstrate various forms of function calls 
//f1(1.2, 1, $obj1, $obj2);// Report an error, number one 2 Parameters are not of array type, Argument 2 passed to f1() must be an array, integer give
//f1(1, $arr, $obj1, $obj1);// Report an error, number one 4 Parameters, Argument 4 passed to f1() must implement interface USB, instance of A given
f1(1.2, $arr, $obj1, $obj2);// No problem 
?>

Run results:

p 1: 1.2 without constraints
p2: Array () that requires an array
Requirements are objects of class A:
object(A)[1]
The requirement is to implement an object that implements the USB interface:
object(B)[2]

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: