The environment I have set up is using MAMP with php version 7.2.14 and PhpStorm. Based on the PHP: Trait - Manual,
traits are mechanisms for code reuse in single inheritance languages...
I have created a ValidationHelper.php
trait to help validate form data. Pear has validation functionality like email($email_addr)
that accepts an email address to validate. Calling this class statically appears to no longer be ideal, so I am attempting to instantiate it. Php throws an error if you attempt to initialize a property with an object, therefore, I added a constructor to instantiate the object. The PHP: Trait - Manual also states that
It is not possible to instantiate a Trait on its own.
On its own being the key phrase for ambiguity. With that being said, how would you add a constructor that initializes some property, or instantiate an object using a trait?
Can Traits have properties...Constructors is where I read that traits can have these members. I do see that they included a constructor, but not 100% sure how it works.
include_once 'Pear/Validate.php';
trait ValidationHelper
{
protected $validate;
public function __constructor(){
$this->validate = new Validate();
}
public function validate_email($email){
return $this->validate->email($email); //$validate is null
}
}