note 86074 added to language.oop5.typehinting

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
TYPE-HINTING and VISIBILITY

Type-hinting is just one more small piece of PHP that protects our objects when visibility cannot.

<?php

class Point {
  public $x, $y;

  public function __construct($xVal = 0, $yVal = 0) {
    $this->x = $xVal;
    $this->y = $yVal;
  }
}

class Polyline {
  protected $points = array();

  public function addPoint(Point $p) {  // the line we're interested in...
    $this->points[] = $p;
  }
}

$point1 = new Point(15, 12);
$polyline = new Polyline();
$polyline->addPoint($point1);
$polyline->addPoint(new Point(55, 22));
$polyline->addPoint(new Point(33, 31));

$polyline->addPoint(new stdClass());    // PHP will throw an error for us!  

?>

Since our Polyline::addPoint() function has to be public, any outside code can try to pass anything. But, when type-hinting is declared, PHP throws an error when phoney data tries to sneak by.
----
Server IP: 69.147.83.197
Probable Submitter: 67.150.1.108
----
Manual Page -- http://www.php.net/manual/en/language.oop5.typehinting.php
Edit        -- https://master.php.net/note/edit/86074
Del: integrated  -- https://master.php.net/note/delete/86074/integrated
Del: useless     -- https://master.php.net/note/delete/86074/useless
Del: bad code    -- https://master.php.net/note/delete/86074/bad+code
Del: spam        -- https://master.php.net/note/delete/86074/spam
Del: non-english -- https://master.php.net/note/delete/86074/non-english
Del: in docs     -- https://master.php.net/note/delete/86074/in+docs
Del: other reasons-- https://master.php.net/note/delete/86074
Reject      -- https://master.php.net/note/reject/86074
Search      -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.