yassineqoraiche
10 months ago

how to get public class properties (not inhertied ones)

Posted 10 months ago by yassineqoraiche

Hi, I want to know how to get only public class properties in the Mailable class (not the inherited ones) for example:

<?php

namespace App\Mail;

use App\Mail\Mailable;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

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

    public $name;
    
    public $body;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */ 
    public function build()
    {
        return $this->view('emails.simpleview')->subject('New Shit');
    }
}

This class (TestMail1) inherits many properties from the extended class (Mailable), but i want only to get the name and body properties defained in the class itself

i try this:

$mailable = (new mailable1);

$data = new ReflectionClass($mailable);

$properties = $data->getProperties(ReflectionProperty::IS_PUBLIC);

$properties_in = [];

foreach ($properties as $prop) {
    if ($prop->class == $data->getName())
     $properties_in[] = $prop->name;
}

dd($properties_in);

but this returns:

array:8 [▼
      0 => "name"
      1 => "body"
      2 => "connection"
      3 => "queue"
      4 => "chainConnection"
      5 => "chainQueue"
      6 => "delay"
      7 => "chained"
    ]

Any solution?

Please sign in or create an account to participate in this conversation.