phpro / soap-client

A general purpose SOAP client for PHP
MIT License
855 stars 175 forks source link

Typed property ... must not be accessed before initialization #513

Closed RiseAndCry closed 4 months ago

RiseAndCry commented 4 months ago

Bug Report

Q A
BC Break no
Version 3.1.2

Summary

When using auto generated classes for response handling, i get this error: Typed property ...\BaseReturnType::$ReturnCode must not be accessed before initialization

Current behavior

Accessing property which has a value set does not work.

How to reproduce

Response class (auto generated):

<?php
declare(strict_types=1);

namespace ...\Type;

abstract class BaseReturnType
{
    private bool $IsOk;

    private bool $IsWarninig;

    private bool $IsError;

    private int $ReturnCode;

    private ?string $ReturnText = null;

    public function getIsOk(): bool
    {
        return $this->IsOk;
    }

    public function withIsOk(bool $IsOk): static
    {
        $new = clone $this;
        $new->IsOk = $IsOk;

        return $new;
    }

    public function getIsWarninig(): bool
    {
        return $this->IsWarninig;
    }

    public function withIsWarninig(bool $IsWarninig): static
    {
        $new = clone $this;
        $new->IsWarninig = $IsWarninig;

        return $new;
    }

    public function getIsError(): bool
    {
        return $this->IsError;
    }

    public function withIsError(bool $IsError): static
    {
        $new = clone $this;
        $new->IsError = $IsError;

        return $new;
    }

    public function getReturnCode(): int
    {
        return $this->ReturnCode;
    }

    public function withReturnCode(int $ReturnCode): static
    {
        $new = clone $this;
        $new->ReturnCode = $ReturnCode;

        return $new;
    }

    public function getReturnText(): ?string
    {
        return $this->ReturnText;
    }

    public function withReturnText(?string $ReturnText): static
    {
        $new = clone $this;
        $new->ReturnText = $ReturnText;

        return $new;
    }
}
  1. Call soap client that will return the aforementioned response
  2. Try calling getReturnCode() on it - it throws Typed property ...\BaseReturnType::$ReturnCode must not be accessed before initialization error.

When i debug this BaseReturnType (after executing request), i get such a response (partial):

Screenshot 2024-05-07 at 18 14 52

(Bankruptcies comes from SubClass)

which means the values have been set (as expected).

Expected behavior

Calling getReturnCode() should return the value instead of throwing an error:


I noticed a few weird things though:

  1. if instead of calling getReturnCode() i directly access the property via $returnType->ReturnCode - it works. But it's a private variable, so... ehh ??? no Cannot access private property ... error ? i'm really confused.
  2. if i add a default value to ReturnCode (999) - it shows up as an additional variable (both in BaseReturnType and in SubClass) ??? Screenshot 2024-05-07 at 18 12 45
veewee commented 4 months ago

Hello There,

Can you validate if the error still exists when you change the property visibility to protected on all classes?

I have a gut feeling it is related to the behaviour decribed here: https://github.com/veewee/reflecta/issues/5

SInce the property is declared private in a child-class, PHP's soap-client will set a public property on the parent type instead. In that case, the error makes sense.

RiseAndCry commented 4 months ago

Yeap, protected properties work ! Thank you very much, this saved me quite a bit of time troubleshooting :)

And thanks for your contributions and such a fast response !

veewee commented 4 months ago

No problem :)


If you like the work we provide here: consider sponsoring us.

veewee commented 2 months ago

Hello there @RiseAndCry ,

I wanted to get back to this issue: We are finishing up v4 of the soap-client package which uses a new encoding system internally. With this new system, you should be able to get around this issue more easily.

We are eager to receive some early feedback on this new release. If you got some time, feel free to play around with the alpha version: