0

I'm used to set and declare my parameters in a php function like this:

function foo( $length = 24 ) { ...

Now I would like to do that with a class-function:

class foo{   
    function configuration() {
      $this->defaultLength = 24;
    }
    function foo() {
      $this->configuration();
    }
    function test($length = $this->defaultLength) {...

by calling:

$r = new foo();
$r->test();

I get this error message: Parse error: syntax error, unexpected '$this' (T_VARIABLE) in /www/htdo..

How can I define and declare a variable in a php class?

4 Answers 4

2

I would do this:

class foo
{   
    const DEFAULT_LENGHT = 24;

    public function test($length = self::DEFAULT_LENGHT)
    {
        var_dump($length);    
    }
}

$foo = new foo();
$foo->test(); // // int(24)

If you need to change that value in runtime, you can do something like this:

class foo
{   
    protected $default_lenght = 24;

    public function setDefaultLenght($lenght)
    {
       $this->default_lenght = $lenght;
    }

    public function test($length = null)
    {
        $length = $length ?: $this->default_lenght;
        var_dump($length);
    }
}

$foo = new foo();
$foo->test(); // int(24)
$foo->setDefaultLenght(42);
$foo->test(); // int(42)
Sign up to request clarification or add additional context in comments.

3 Comments

@E_p, my bad, sorry I was distracted
NP add public before test function and code review passed :).
This really hasn't been my day, I swear that I always follow PSR-2 specification :P
1

You can't reference $this in your function signature. You could do something like:

public function test ($length = null) {
    if ($length === null) {
        $length = $this->defaultLength;
    }
}

2 Comments

Why can't he? if he wants to use it as $r->test(); then $this->defaultLength is accessible if he declares it in the same Scope (inside the class).
Inside the function, yes. But not inside the function signature, since the function signature is read before any instance is created.
1

You could build an array with the element of the signature of your function, filter them and merge it with a default configuration calling all the functions you need. I made a simple example with defining 2 parameter with different functions.

class foo{   

    private $configuration;

    private function buildDefaultConfiguration() {
        return $configuration = array(
            'defaultLength' => $this->getDefaultLength(),
            'defaultWidth'  => $this->getDefaultWidth(),
        );
    }

    public function __construct($givenLength = null, $givenWidth = null) {
        $givenConfiguration = array(
            'defaultLength' => $givenLength,
            'defaultWidth'  => $givenWidth
        );
        $givenConfiguration = array_filter($givenConfiguration);
        $defaultConfiguration = $this->buildDefaultConfiguration();
        $this->configuration = array_merge($defaultConfiguration, $givenConfiguration);
    }

    public function test()
    {
        echo $this->configuration['defaultLength'] . ' - ' . $this->configuration['defaultWidth'];
    }

    private function getDefaultLength()
    {
        return 24;
    }

    private function getDefaultWidth()
    {
        return 12;
    }
}

$foo1 = new foo(13,14);
$foo2 = new foo();
$foo3 = new foo(null, 66);
$foo4 = new foo(66);

$foo1->test();
//result is 13 - 14
$foo2->test();
//result is 24 - 12
$foo3->test();
//result is 24 - 66
$foo4->test();
//result is 66 - 12

You can check a live working example here, formatting isn't great though Hope this'll help you

Comments

0
class foo{  
//set the default lenght first 
var $defaultLength = 0;

function configuration() {
  $this->defaultLength = 24;
}


function setLength($newLength){
 $this->defaultLength = $newLength;
}

function test($length = null) {
 //if length is null assign default
 if($length == null){
$length = $this->defaultLength; 
 }

}

$foo = new foo();
$this->configuration();
$foo->test(); // int(24)
$foo->setLength(42);
$foo->test(); // int(42)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.