現在の言語: 日本語 |
配列型 |
php型宣言 |
class test1
{
public int $num = 0;
public string $str = "";
function test1():void
{
$person = [
'name' => 'Taro Yamada',
'age' => 25,
];
$this->test2($person);
//関数呼び出し後の外部での age の表示
echo "age:" . $person['age'] . PHP_EOL;//age:25
}
//引数に値渡しを使用したケース
private function test2(array $ary):void
{
//関数内部で変更してみる
$ary['age'] = $ary['age'] + 1;
// $ary を使って配列の要素にアクセスする
echo "名前は" . $ary['name'] . PHP_EOL;//名前はTaro Yamada
echo "来年は" . $ary['age'] . "歳です。" . PHP_EOL;//来年は26歳です。
}
function test3():void
{
$person = [
'name' => 'Taro Yamada',
'age' => 25,
];
$this->test2($person);
//関数呼び出し後の外部での age の表示
echo "age:" . $person['age'] . PHP_EOL;//age:25
}
//引数に&(参照渡し)を使用したケース
private function test4(array &$ary):void
{
//関数内部で変更してみる
$ary['age'] = $ary['age'] + 1;
// $ary を使って配列の要素にアクセスする
echo "名前は" . $ary['name'] . PHP_EOL;//名前はTaro Yamada
echo "来年は" . $ary['age'] . "歳です。" . PHP_EOL;//来年は26歳です。
}
}
echo "<pre>";
$cls1= new test1();
$cls1->test1();//値渡しのケース
$cls1->test3();//参照渡しのケース
$cls = new test1();
$cls->num = 100;
$cls->str = "test";
function test5(test1 $cls)
{
$cls->num = $cls->num + 1;
$cls->str = $cls->str . " add";
}
test5($cls);
echo $cls->num.PHP_EOL;//101
echo $cls->str.PHP_EOL;//test add
echo "</pre>";
class test1
{
public int $num = 0;
public string $str = "";
function test1():void
{
$person = [
'name' => 'Taro Yamada',
'age' => 25,
];
$this->test2($person);
// Displaying age outside the function call
echo "age:" . $person['age'] . PHP_EOL;//age:25
}
// Example of passing arguments by value
private function test2(array $ary):void
{
// Try changing it inside the function
$ary['age'] = $ary['age'] + 1;
// Accessing an array element using $ary
echo "My name is" . $ary['name'] . PHP_EOL;//My name is Taro Yamada
echo "Next year I will be" . $ary['age'] . "years old." . PHP_EOL;//I'll be 26 next year.
}
function test3():void
{
$person = [
'name' => 'Taro Yamada',
'age' => 25,
];
$this->test2($person);
// Displaying age outside the function call
echo "age:" . $person['age'] . PHP_EOL;//age:25
}
// Example of using & (passed by reference) as an argument
private function test4(array &$ary):void
{
// Try changing it within the function
$ary['age'] = $ary['age'] + 1;
// Accessing an array element using $ary
echo "My name is" . $ary['name'] . PHP_EOL;//My name is Taro Yamada
echo "Next year I will be" . $ary['age'] . "years old." . PHP_EOL;//I will be 26 next year.
}
}
echo "<pre>";
$cls1= new test1();
$cls1->test1();//Pass by value
$cls1->test3();//Pass by reference
$cls = new test1();
$cls->num = 100;
$cls->str = "test";
function test5(test1 $cls)
{
$cls->num = $cls->num + 1;
$cls->str = $cls->str . " add";
}
test5($cls);
echo $cls->num.PHP_EOL;//101
echo $cls->str.PHP_EOL;//test add
echo "</pre>";
class test2
{
private function test1()
{
try
{
$int = 100;
//Fatal error: Uncaught TypeError: test2::testArrayArguments(): Argument #1 ($ary) must be of type array, int given, called
$this->testArrayArguments($int);
}catch(Exception $e){
echo $e->getMessage();
}
finally
{
}
}
private function test2()
{
try
{
//Fatal error: Uncaught TypeError: test2::testArrayArguments(): Argument #1 ($ary) must be of type array, null given, called
$null = null;
$this->testArrayArguments($null);
}catch(Exception $e){
echo $e->getMessage();
}
finally
{
}
}
private function test3()
{
try
{
//Fatal error: Uncaught TypeError: test2::testArrayArguments(): Argument #1 ($ary) must be of type array, string given, called
$str = "test";
$this->testArrayArguments($str);
}catch(Exception $e){
echo $e->getMessage();
}
finally
{
}
}
//引数に配列を使う関数
private function testArrayArguments(array $ary)
{
echo "execute testArrayArguments".PHP_EOL;
}
//nullable型を使用したケース
private function test4(?array $ary)
{
if(is_array($ary))
{
echo "配列です".PHP_EOL;
}
else
{
echo "配列ではありません".PHP_EOL;
}
}
private function test5(array $ary =[])
{
if(empty($ary))
{
echo "空です";
return;
}
foreach($ary as $data)
{
echo $data.PHP_EOL;
}
}
function test()
{
//$this->test1();//int
//$this->test2();//null
//$this->test3();//string
$int = 100;
//Fatal error: Uncaught TypeError: test2::test4(): Argument #1 ($ary) must be of type ?array, int given, called
//$this->test4($int);
$ary=[];
$ary[]=1;
$ary[]=2;
//echoの出力結果
/*
1
2
*/
$this->test5($ary);
$ary=[];
$this->test5($ary);
//echoの出力結果
//空です
}
}
echo "<pre>";
$cls2 = new test2();
$cls2->test();
echo "</pre>";
class test2
{
private function test1()
{
try
{
$int = 100;
//Fatal error: Uncaught TypeError: test2::testArrayArguments(): Argument #1 ($ary) must be of type array, int given, called
$this->testArrayArguments($int);
}catch(Exception $e){
echo $e->getMessage();
}
finally
{
}
}
private function test2()
{
try
{
//Fatal error: Uncaught TypeError: test2::testArrayArguments(): Argument #1 ($ary) must be of type array, null given, called
$null = null;
$this->testArrayArguments($null);
}catch(Exception $e){
echo $e->getMessage();
}
finally
{
}
}
private function test3()
{
try
{
//Fatal error: Uncaught TypeError: test2::testArrayArguments(): Argument #1 ($ary) must be of type array, string given, called
$str = "test";
$this->testArrayArguments($str);
}catch(Exception $e){
echo $e->getMessage();
}
finally
{
}
}
// Function using an array as an argument
private function testArrayArguments(array $ary)
{
echo "execute testArrayArguments".PHP_EOL;
}
// Example using a nullable type
private function test4(?array $ary)
{
if(is_array($ary))
{
echo "It's an array".PHP_EOL;
}
else
{
echo "It's not an array".PHP_EOL;
}
}
private function test5(array $ary =[])
{
if(empty($ary))
{
echo "It's empty";
return;
}
foreach($ary as $data)
{
echo $data.PHP_EOL;
}
}
function test()
{
//$this->test1();//int
//$this->test2();//null
//$this->test3();//string
$int = 100;
//Fatal error: Uncaught TypeError: test2::test4(): Argument #1 ($ary) must be of type ?array, int given, called
//$this->test4($int);
$ary=[];
$ary[]=1;
$ary[]=2;
//echo output result
/*
1
2
*/
$this->test5($ary);
$ary=[];
$this->test5($ary);
//echo output result
//Empty
}
}
echo "<pre>";
$cls2 = new test2();
$cls2->test();
echo "</pre>";
| 著作権情報 |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |