Funktsiyani PHP-dagi boshqa funktsiyadan chaqiring

Quyidagi strukturaga ega bo'lgan PHP kodini amalga oshirish uchun kurashaman:

public function hookActionValidateOrder($params)
{
     $invoice = new Address((int)$order->id_address_invoice);
     $myStreet = $invoice->address1;
     $myCity = $invoice->city;
     $myPostcode = $invoice->postcode;

    //... SOME IRRELEVANT CODE HERE ...

     $Tid = send($myStreet, $myCity, $myPostcode); /* Calling function send($a, $b, $c) */
}

public function send($a, $b, $c)    /* function send($a, $b, $c) */
{
    //... CODE TO DO SOMETHING USING VARIABLES $a, $b, $c ...
}

Muammo shundaki, bu kod ishlamayapti. Kodni tekshirgichga qo'yganimda, u shunday deydi: "Function" send() "mavjud emas" . Menga ayting-chi, nega shunday ekan va buni qanday qilib tuzataman?

1
har qanday ramka ishlatasizmi?
qo'shib qo'ydi muallif prakash tank, manba
sinfda joylashgan bu ikkita vazifani bajaradimi?
qo'shib qo'ydi muallif Yaman Jain, manba
sinfda joylashgan bu ikkita vazifani bajaradimi?
qo'shib qo'ydi muallif Yaman Jain, manba

7 javoblar

Sizda funktsiyani qidirish uchun $ this dan foydalanishingiz mumkin

class Test {

    public function say($a) {
        return $a ;

    } 

    public function tell() {
        $c = "Hello World" ;
        $a = $this->say($c) ;
        return $a ;
    }
} 

$b= new Test() ;   
echo $b->tell()  ;

Agar siz foydalanishni yopishdan ko'ra oddiy funktsiyadan foydalansangiz

function tell(){
   $a = "Hello" ;
   return function($b) use ($a){
      retrun $a." ".$b ;
   } ; 
}

$s = tell() ;
echo $s("World")

;

2
qo'shib qo'ydi

Sizda funktsiyani qidirish uchun $ this dan foydalanishingiz mumkin

class Test {

    public function say($a) {
        return $a ;

    } 

    public function tell() {
        $c = "Hello World" ;
        $a = $this->say($c) ;
        return $a ;
    }
} 

$b= new Test() ;   
echo $b->tell()  ;

Agar siz foydalanishni yopishdan ko'ra oddiy funktsiyadan foydalansangiz

function tell(){
   $a = "Hello" ;
   return function($b) use ($a){
      retrun $a." ".$b ;
   } ; 
}

$s = tell() ;
echo $s("World")

;

2
qo'shib qo'ydi

harakat qilib ko'ring:

class test 
{
public function hookActionValidateOrder($params)
    {
         $invoice = new Address((int)$order->id_address_invoice);
         $myStreet = $invoice->address1;
         $myCity = $invoice->city;
         $myPostcode = $invoice->postcode;

        //... SOME IRRELEVANT CODE HERE ...

         $Tid = send($myStreet, $myCity, $myPostcode); /* Calling function send($a, $b, $c) */
    }
  }
class test1 extends test
{
    public function send($a, $b, $c)    /* function send($a, $b, $c) */
    {
        //... CODE TO DO SOMETHING USING VARIABLES $a, $b, $c ...
    }
}

Buni bir klassni boshqa sinfga uzaytirish bilan hal qilish mumkin.

0
qo'shib qo'ydi
Uzr, mening xatolarim yangilandi. taklifingizga rahmat :)
qo'shib qo'ydi muallif Gulshan, manba
u kengayib bormaydi
qo'shib qo'ydi muallif Adhan Timothy Younes, manba

harakat qilib ko'ring:

class test 
{
public function hookActionValidateOrder($params)
    {
         $invoice = new Address((int)$order->id_address_invoice);
         $myStreet = $invoice->address1;
         $myCity = $invoice->city;
         $myPostcode = $invoice->postcode;

        //... SOME IRRELEVANT CODE HERE ...

         $Tid = send($myStreet, $myCity, $myPostcode); /* Calling function send($a, $b, $c) */
    }
  }
class test1 extends test
{
    public function send($a, $b, $c)    /* function send($a, $b, $c) */
    {
        //... CODE TO DO SOMETHING USING VARIABLES $a, $b, $c ...
    }
}

Buni bir klassni boshqa sinfga uzaytirish bilan hal qilish mumkin.

0
qo'shib qo'ydi
Uzr, mening xatolarim yangilandi. taklifingizga rahmat :)
qo'shib qo'ydi muallif Gulshan, manba
u kengayib bormaydi
qo'shib qo'ydi muallif Adhan Timothy Younes, manba

harakat qilib ko'ring:

class test 
{
public function hookActionValidateOrder($params)
    {
         $invoice = new Address((int)$order->id_address_invoice);
         $myStreet = $invoice->address1;
         $myCity = $invoice->city;
         $myPostcode = $invoice->postcode;

        //... SOME IRRELEVANT CODE HERE ...

         $Tid = send($myStreet, $myCity, $myPostcode); /* Calling function send($a, $b, $c) */
    }
  }
class test1 extends test
{
    public function send($a, $b, $c)    /* function send($a, $b, $c) */
    {
        //... CODE TO DO SOMETHING USING VARIABLES $a, $b, $c ...
    }
}

Buni bir klassni boshqa sinfga uzaytirish bilan hal qilish mumkin.

0
qo'shib qo'ydi
Uzr, mening xatolarim yangilandi. taklifingizga rahmat :)
qo'shib qo'ydi muallif Gulshan, manba
u kengayib bormaydi
qo'shib qo'ydi muallif Adhan Timothy Younes, manba

agar biror funktsiyani yoki asl sahifadagi har qanday kodni ikkinchi sahifada tarkibiga kiritmasdan ishlatmoqchi bo'lsangiz, unda odatda bu sizga xato

(.... 'string nomi, funktsiya nomi, ....... va boshqalar) mavjud emas


The solution is to include it first in your secondary page and use :

include("yourpage.php");

if not then
see "@Gulshan" Comment about "extends" classes. it causes the same problem

0
qo'shib qo'ydi

agar biror funktsiyani yoki asl sahifadagi har qanday kodni ikkinchi sahifada tarkibiga kiritmasdan ishlatmoqchi bo'lsangiz, unda odatda bu sizga xato

(.... 'string nomi, funktsiya nomi, ....... va boshqalar) mavjud emas


The solution is to include it first in your secondary page and use :

include("yourpage.php");

if not then
see "@Gulshan" Comment about "extends" classes. it causes the same problem

0
qo'shib qo'ydi
PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin