PHP-de faýllary ýekeje gezek birikdirmek
Bizde pow.php atly faýl bolsun, onda ýygnam funksiýalar saklanylsyn:
<?php
function square($num) {
return $num ** 2;
}
function cube() {
return $num ** 3;
}
?>
Pow.php faýlynyň funksiýalaryny sum.php faýlynda ulandygymyz bolsun:
<?php
require 'pow.php';
function squareSum($arr) {
$res = 0;
foreach ($arr as $elem) {
$res += square($elem);
}
return $res;
}
function cubeSum($arr) {
$res = 0;
foreach ($arr as $elem) {
$res += cube($elem);
}
return $res;
}
?>
Esasy faýlda iki faýly hem birikdirýändigimiz bolsun:
<?php
require 'pow.php';
require 'sum.php';
echo square(3) + squareSum([1, 2, 3]);
?>
Emma, biziň öňümizde bir meselä çykýar.
index.php faýlyna pow.php
faýly iki gezek birikdiriler: özi we
sum.php faýly arkaly.
Bu meselä sebäp bolup biler, sebäbi bizde şol bir atly iki set funksiýa bolar.
Meseläni çözmek üçin ähli faýllary
require_once operator arkaly
birikdirmeli - ol faýly diňe bir gezek
birikdirer we gaýtalanan birikdirilmeleri
seretmez:
<?php
require_once 'pow.php';
require_once 'sum.php';
echo square(3) + squareSum([1, 2, 3]);
?>
Paydaly funksiýa toplumlary bilen birnäçe faýl düzyň. Bu faýllary biri-biriňize we esasy faýlyňyza birikdiriň.