Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dan3460's avatar

Weird Problem on a Class

I'm working on some simulations, which run some jobs. One of them is crashing because is trying to get a property from a non object. Here is the job:

class TransactCompanyProduction implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $company;
    private $inventories;
    private $materials;
    private $universe;
    private $inventoryAccount;
    private $cogAccount;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Company $company)
    {
        echo "Construct";
        $this->$company = $company;
        echo $this->$company;
        $this->inventories = $company->inventory;
        $this->materials = $company->product->composition;
        $this->universe = Universe::first();
        $this->inventoryAccount = Account::where('name', 'Inventory')->first()->id;
        $this->cogAccount = Account::where('name', 'COG')->first()->id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        echo "Handle";
        echo $this->company;
        $fullProduction = $this->company->product->production;
        $fullEmployees = $this->company->employees->sum('positions_required');
        $totalEmployees = $this->company->employees->sum('positions_filled');
        $d = $totalEmployees / $fullEmployees; // employee deficit in %
        $maxProduction = $totalEmployees == $fullEmployees ? $fullProduction : $fullProduction * (pow($d, 2) * 0.0108 - $d * 0.1047 + 2.5752) / 100;
        foreach ($this->materials as $material) {
            $maxProductionByInventory =
                $this->inventories->where('product_id', $material->product_id)->first()->units ?? 0 / $material->units;
            $maxProduction = $maxProduction <= $maxProductionByInventory ? $maxProduction : $maxProductionByInventory;
            if ($maxProduction == 0) {
                return;
            }
        }
        $this->executeProduction($maxProduction);
    }

i have a few echo in the construct and on the handle and here is the result when running the job from tinker:

>>> \Bus::dispatch(new ProcessCountryProduction($c))
[!] Aliasing 'ProcessCountryProduction' to 'App\Jobs\ProcessCountryProduction' for this Tinker session.
Construct{"id":1,"name":"Mosciski, Quitzon and VonRueden","country_id":1,"owner_id":1,"product_id":1,"company_type_id":2,"cost":100,"markup":20,"terms":30,"current
_ratio":0,"acid_test":0,"operating_cash_flow":0,"debt_ratio":0,"debt_to_equity":0,"interest_coverage":0,"debt_service":0,"asset_turnover":0,"inventory_turnover":0,
"receivable_turnover":0,"day_sales":0,"gross_margin":0,"operating_margin":0,"return_on_assets":0,"book_value_per_share":0,"dividend_yield":0,"created_at":"2021-09-
12T18:49:12.000000Z","updated_at":"2021-09-12T18:49:12.000000Z"}Handle<warning>PHP Notice:  Trying to get property 'product' of non-object in C:\Users\Daniel.SFP\D
ocuments\Projects\worldmarket\app\Jobs\TransactCompanyProduction.php on line 53</warning>
<warning>PHP Notice:  Trying to get property 'production' of non-object in C:\Users\Daniel.SFP\Documents\Projects\worldmarket\app\Jobs\TransactCompanyProduction.ph
p on line 53</warning>
<warning>PHP Notice:  Trying to get property 'employees' of non-object in C:\Users\Daniel.SFP\Documents\Projects\worldmarket\app\Jobs\TransactCompanyProduction.php
 on line 54</warning>
⏎
PHP Error:  Call to a member function sum() on null in C:\Users\Daniel.SFP\Documents\Projects\worldmarket\app\Jobs\TransactCompanyProduction.php on line 54
>>>

In the handle "$this->company" is empty. Another clue, i'm using VSC if i click once on the $company on my declarations VSC highlights all the places where the variable is used except inside the construct. I tried on a few other classes and VSC will highlight all keywords regardless if there inside the construct.

i'm lost here.

0 likes
2 replies
MichalOravec's avatar
Level 75

It has to be like this in the constructor.

$this->company = $company;

and not

$this->$company = $company;
       ^ this dollar sign is wrong
dan3460's avatar

That is obvious :(, i have been looking at this thing for two hours and could not see. Thanks very much.

Please or to participate in this conversation.