October 22, 2024
Chicago 12, Melborne City, USA
PHP

Why does my array fires Error: Call to undefined method stdClass on a function after json encode / decode?


I am working on an object I want to send to another php script via Ajax to perform a function called lire()

So to send this object called Caddie, I json_encode it and then in my php script json_decode it to call it with the function lire.

But after it is decoded, the function to call my object gives me the error

Error: Call to undefined method stdClass::lire()

Here is the oject:

object(stdClass)[14]
  public 'id_client' => string 'ej52e59atvl4hdeu0srqatkl1b' (length=26)
  public 'lignes' => 
    array (size=1)
      0 => 
        object(stdClass)[12]
          public 'id' => string '130' (length=3)
          public 'id_produit' => string '1010' (length=4)
          public 'reference' => string '' (length=0)
          public 'url_produit' => string 'deontologie-de-lagent-immobilier' (length=32)
          public 'article' => string 'Déontologie de l'agent immobilier' (length=34)
          public 'quantite' => string '1.000' (length=5)
          public 'prix_TTC' => string '25.00' (length=5)
          public 'prix_HT' => string '25.00' (length=5)
          public 'prix_TVA' => int 0
          public 'taux_TVA' => null
          public 'montant_ligne_HT' => int 25
          public 'montant_ligne_TTC' => int 25
          public 'montant_ligne_TVA' => int 0
  public 'infos' => 
    object(stdClass)[15]
      public 'nb' => int 1
      public 'reste' => int 0
      public 'poids_total' => int 0
      public 'total_TTC' => int 25
      public 'total_HT' => int 25
      public 'total_TVA' => int 0
      public 'montant_TTC' => int 25
      public 'montant_HT' => int 25
      public 'montant_TVA' => int 0
      public 'remise_code_promo_TTC' => int 0
      public 'remise_code_promo_HT' => int 0
      public 'remise_code_promo_TVA' => int 0
      public 'fdp_TTC' => int 0
      public 'fdp_HT' => int 0
      public 'fdp_TVA' => int 0
  public 'id_pays' => boolean false
  public 'id_mode_transport' => boolean false
  public 'id_code_promo' => int 0
  public 'code_promo' => boolean false
  public 'remise_code_promo' => int 0
  public 'type_remise_code_promo' => int 0

And here is the simplified process in php without ajax, to reproduce the problem:

Here I take the original object to use my function lire()

function bouton_paypal_checkout($Caddie) {
    while ($row = $Caddie->lire()) {
        // Traitement de $row
        print_r($row); // Pour afficher les données, par exemple
    }
}

Which prints the right rows from my object with the function lire:

Array ( https://stackoverflow.com/q/79094411 => 130 [id_produit] => 1010 [reference] => [url_produit]
=> deontologie-de-lagent-immobilier [article] => Déontologie de l’agent immobilier [quantite] => 1.000 [prix_TTC] => 25.00 [prix_HT]
=> 25.00 [prix_TVA] => 0 [taux_TVA] => [montant_ligne_HT] => 25 [montant_ligne_TTC] => 25 [montant_ligne_TVA] => 0 )

But now to reproduce the buggy phenomenon I try to encode then decode the object, the same way I would do with Ajax:

function bouton_paypal_checkout($Caddie) {
    // Encoder l'objet Caddie en JSON
    $Caddie_ajax = json_encode($Caddie);    
    // Décoder le JSON en objet (pas de true ici)
    $Caddie_arr = json_decode($Caddie_ajax);
    while ($row = $Caddie_arr->lire()) {
        // Traitement de $row
        print_r($row); // Pour afficher les données, par exemple
    }
}

And there comes the error Uncaught Error: Call to undefined method stdClass::lire()

How come my object isn’t readable anymore? Is there an alternative to json encode / decode I could use to send my data via Ajax without corrupting my object?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video