Search code examples
phpfedex

PHP Class Value


I'm working on a PHP Class, one of the required fields for the Class is (DropoffType) The answer to that is REGULARPICKUP. Can I write this inside the class and put "=" to the REGULARPICKUP?

Example:

protected $DropoffType = 'REGULARPICKUP';

Solution

  • Sure you can

    class MyClass
    {
        protected $DropoffType = 'REGULARPICKUP';
    }
    

    or do it in the constructor (this depends on your specific case whether this is the way to go):

    class MyClass
    {
        protected $DropoffType;
    
        public function __construct($Dropofftype = 'REGULARPICKUP')
        {
            $this->DropoffType = $Dropofftype;
        }
    }
    

    Or if you have a set function:

    class MyClass
    {
        protected $DropoffType;
    
        public function setDropoffType($Dropofftype = 'REGULARPICKUP')
        {
            $this->DropoffType = $Dropofftype;
        }
    }