1
0
mirror of https://github.com/php/doc-en.git synced 2026-03-23 23:32:18 +01:00

Fixed examples, and made some self-contained

This commit is contained in:
Derick Rethans
2025-03-06 14:33:15 +00:00
parent 5003a6ea92
commit d6f54016d6
20 changed files with 100 additions and 37 deletions

View File

@@ -185,9 +185,8 @@ class@anonymous/in/oNi1A0x7f8636ad2021
<programlisting role="php">
<![CDATA[
<?php
// Using an anonymous class
$util->setLogger(new readonly class('[DEBUG]') {
var_dump(new readonly class('[DEBUG]') {
public function __construct(private string $prefix)
{
}

View File

@@ -224,11 +224,16 @@ readonly class Foo
<programlisting role="php">
<![CDATA[
<?php
class SimpleClass {
}
$instance = new SimpleClass();
var_dump($instance);
// This can also be done with a variable:
$className = 'SimpleClass';
$instance = new $className(); // new SimpleClass()
var_dump($instance);
?>
]]>
</programlisting>
@@ -296,6 +301,9 @@ object(ClassD)#1 (0) {
<programlisting role="php">
<![CDATA[
<?php
class SimpleClass {
public string $var;
}
$instance = new SimpleClass();
@@ -375,16 +383,18 @@ bool(true)
<programlisting role="php">
<![CDATA[
<?php
echo (new DateTime())->format('Y');
echo (new DateTime())->format('Y'), PHP_EOL;
// surrounding parentheses are optional as of PHP 8.4.0
echo new DateTime()->format('Y');
echo new DateTime()->format('Y'), PHP_EOL;
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
2016
2025
2025
]]>
</screen>
</example>
@@ -498,6 +508,14 @@ echo ($obj->bar)(), PHP_EOL;
<programlisting role="php">
<![CDATA[
<?php
class SimpleClass
{
function displayVar()
{
echo "Parent class\n";
}
}
class ExtendClass extends SimpleClass
{
// Redefine the parent method
@@ -757,9 +775,10 @@ Does\Not\Exist
namespace NS {
class ClassName {
}
$c = new ClassName();
print $c::class;
}
$c = new ClassName();
print $c::class;
?>
]]>
</programlisting>

View File

@@ -236,6 +236,9 @@ function test(
<programlisting role="php">
<![CDATA[
<?php
$some_json_string = '{ "id": 1004, "name": "Elephpant" }';
$some_xml_string = "<animal><id>1005</id><name>Elephpant</name></animal>";
class Product {
private ?int $id;
@@ -256,12 +259,11 @@ class Product {
return new static($data['id'], $data['name']);
}
public static function fromXml(string $xml): static {
// Custom logic here.
$data = convert_xml_to_array($xml);
public static function fromXml(string $xml): static {a
$data = simplexml_load_string($xml);
$new = new static();
$new->id = $data['id'];
$new->name = $data['name'];
$new->id = $data->id;
$new->name = $data->name;
return $new;
}
}
@@ -269,6 +271,8 @@ class Product {
$p1 = Product::fromBasicData(5, 'Widget');
$p2 = Product::fromJson($some_json_string);
$p3 = Product::fromXml($some_xml_string);
var_dump($p1, $p2, $p3);
]]>
</programlisting>
</example>

View File

@@ -186,8 +186,6 @@ class PropertyTest
}
echo "<pre>\n";
$obj = new PropertyTest;
$obj->a = 1;

View File

@@ -61,6 +61,10 @@ echo MyClass::CONST_VALUE;
<programlisting role="php">
<![CDATA[
<?php
class MyClass {
const CONST_VALUE = 'A constant value';
}
class OtherClass extends MyClass
{
public static $my_static = 'static var';

View File

@@ -220,6 +220,7 @@ class Example
</simpara>
<programlisting role="php">
<![CDATA[
<?php
class Example
{
public function __construct(
@@ -241,6 +242,7 @@ class Example
</simpara>
<programlisting role="php">
<![CDATA[
<?php
class Example
{
public private(set) DateTimeInterface $created {

View File

@@ -52,7 +52,7 @@
<![CDATA[
<?php
$dom = new DOMDocument('1.0', 'iso-8859-1');
$dom = new DOMDocument('1.0', 'utf-8');
$element = $dom->appendChild(new DOMElement('root'));
$attr = $element->setAttributeNode(new DOMAttr('attr', 'attrvalue'));
echo $dom->saveXML();
@@ -64,7 +64,7 @@ echo $dom->saveXML();
<screen>
<![CDATA[
<?xml version="1.0" encoding="utf-8"?>
<root attr="attrvalue" />
<root attr="attrvalue"/>
]]>
</screen>
</example>

View File

@@ -34,7 +34,7 @@
$doc = new DOMDocument;
$doc->loadXML("<container><hello/></container>");
$doc->replaceWith("beautiful", $doc->createElement("world"));
$doc->replaceChildren("beautiful", $doc->createElement("world"));
echo $doc->saveXML();
?>

View File

@@ -36,7 +36,7 @@ $doc->loadXML("<container><hello/></container>");
$fragment = $doc->createDocumentFragment();
$fragment->append("hello");
$fragment->replaceWith("beautiful", $doc->createElement("world"));
$fragment->replaceChildren("beautiful", $doc->createElement("world"));
echo $doc->saveXML($fragment);
?>

View File

@@ -94,8 +94,8 @@ $context = stream_context_create($opts);
libxml_set_streams_context($context);
// request a file through HTTP
$doc = DOMDocument::load('http://www.example.com/file.xml');
$dom = new DOMDocument;
$doc = $dom->load('http://www.example.com/file.xml');
?>
]]>
</programlisting>

View File

@@ -116,9 +116,9 @@
<?php
$str = "mary had a Little lamb and she loved it so";
$str = mb_convert_case($str, MB_CASE_UPPER, "UTF-8");
echo $str; // Prints MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
echo $str, PHP_EOL;
$str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
echo $str; // Prints Mary Had A Little Lamb And She Loved It So
echo $str, PHP_EOL;
?>
]]>
</programlisting>
@@ -133,9 +133,9 @@ echo $str; // Prints Mary Had A Little Lamb And She Loved It So
<?php
$str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός";
$str = mb_convert_case($str, MB_CASE_UPPER, "UTF-8");
echo $str; // Prints ΤΆΧΙΣΤΗ ΑΛΏΠΗΞ ΒΑΦΉΣ ΨΗΜΈΝΗ ΓΗ, ΔΡΑΣΚΕΛΊΖΕΙ ΥΠΈΡ ΝΩΘΡΟΎ ΚΥΝΌΣ
echo $str, PHP_EOL;
$str = mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
echo $str; // Prints Τάχιστη Αλώπηξ Βαφήσ Ψημένη Γη, Δρασκελίζει Υπέρ Νωθρού Κυνόσ
echo $str, PHP_EOL;
?>
]]>
</programlisting>

View File

@@ -104,7 +104,7 @@
<para>
<example>
<title><parameter>map</parameter> example</title>
<programlisting role="php">
<programlisting>
<![CDATA[
<?php
$convmap = array (

View File

@@ -107,7 +107,7 @@
<para>
<example>
<title><parameter>map</parameter> example</title>
<programlisting role="php">
<programlisting>
<![CDATA[
<?php
$convmap = array (

View File

@@ -76,9 +76,12 @@
<programlisting role="php">
<![CDATA[
<?php
// return all array elements
// containing floating point numbers
$array = [ "4", M_PI, "2.74", 42 ];
// return all array elements containing floating point numbers
$fl_array = preg_grep("/^(\d+)?\.\d+$/", $array);
var_dump($fl_array);
?>
]]>
</programlisting>

View File

@@ -316,6 +316,8 @@ if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
echo "A match was not found.";
}
echo "\n";
if (preg_match("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
echo "A match was found.";
} else {

View File

@@ -214,6 +214,15 @@ The bear black slow jumps over the lazy dog.
<programlisting role="php">
<![CDATA[
<?php
$string = 'The quick brown fox jumps over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);

View File

@@ -243,7 +243,11 @@ bool(false)
<title>Using XPath</title>
<simpara>
SimpleXML includes built-in <acronym>XPath</acronym> support.
To find all <literal>&lt;character&gt;</literal> elements:
To find all <literal>&lt;character&gt;</literal> elements.
</simpara>
<simpara>
'<literal>//</literal>' serves as a wildcard. To specify absolute
paths, omit one of the slashes:
</simpara>
<programlisting role="php">
<![CDATA[
@@ -258,10 +262,6 @@ foreach ($movies->xpath('//character') as $character) {
?>
]]>
</programlisting>
<simpara>
'<literal>//</literal>' serves as a wildcard. To specify absolute
paths, omit one of the slashes.
</simpara>
&example.outputs;
<screen>
<![CDATA[

View File

@@ -119,7 +119,19 @@ echo $xml->asXML();
<programlisting role="php">
<![CDATA[
<?php
// Continued from example XML above.
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
/* Search for <a><b><c> */
$result = $xml->xpath('/a/b/c');

View File

@@ -78,7 +78,12 @@
<?php
$xmlElement = new SimpleXMLElement('<books><book>PHP basics</book><book>XML basics</book></books>');
echo var_dump($xmlElement->key());
try {
echo var_dump($xmlElement->key());
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
$xmlElement->rewind(); // rewind to the first element
echo var_dump($xmlElement->key());
@@ -88,7 +93,7 @@ echo var_dump($xmlElement->key());
&example.outputs;
<screen>
<![CDATA[
bool(false)
Iterator not initialized or already consumed
string(4) "book"
]]>
</screen>

View File

@@ -108,7 +108,13 @@
<?php
$stream = fopen('large.xml', 'r');
$parser = xml_parser_create();
// set up the handlers here
xml_set_element_handler(
$parser,
function($parser, $name, $attributes) { echo $name, PHP_EOL; },
function($parser, $name) { echo $name, PHP_EOL; }
);
while (($data = fread($stream, 16384))) {
xml_parse($parser, $data); // parse the current chunk
}