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

somenet77's avatar

PHP named argument issue?

I have the following PHP

<?php

namespace App\Mail;

use App\Models\Warranty;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class WarrantyNotification extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     */
    public function __construct(
        public Warranty $warranty,
        public string $subject,
        public string $content,
    ) {
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: $this->subject,
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'emails.warranties.template',
            with: [
                'template' => $this->content,
                'data' => [
                    'name' => $this->warranty->name,
                    'mobile' => $this->warranty->mobile,
                    'email' => $this->warranty->email,
                    'address' => $this->warranty->address,
                    'reference_number' => $this->warranty->reference_number,
                    'product_name' => $this->warranty->product_name,
                    'invoice_number' => $this->warranty->invoice_number,
                    'store_name' => $this->warranty->store_name,
                    'invoice_date' => $this->warranty->invoice_date,
                ],
            ]
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [
            //
        ];
    }
}

In this code, the issue is when I define

public string $subject

It's not working

Type of App\Mail\WarrantyNotification::$subject must not be defined

If I renamed the subject into any other name then it worked fine.

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

The base Mailable class you extend does not define a type for $subject; so you cannot.

1 like
kokoshneta's avatar

Just to make @tykus’ point completely clear: that means you can do public $subject (but you don’t need to, since it’s already there in the parent class), but you cannot do public string $subject.

This is an inherent part of PHP object inheritance.

Please or to participate in this conversation.