Extracing Fields

When you implement an interface with loads of getters/setters can you include a private module level $_field ? 

e.g. getId()/setId() accessing private $_Id?

0
6 comments

I mean automatically without having to type them all out.

0

Hi there,

Can you please clarify what needs to be done (on some very basic example: here what I have and here is what I want to have; as a code so it can be copy-pasted)?

It's not clear at all what you wish to be done here: if you want to generate setters/getters or something else.

0

So I implement an interface (e.g. ProductInterface) using Code/Implement Methods. The product interface has say 20/30 properties each with a getId()/setId(), getSku()/setSku(). I would like generate at the module level "private $_Id; private $_Sku;" and in each of the getters/setters 

[code]

private $_Sku;
public function getSku()
{
return $this->_sku;
}

/**
* Set product sku
*
* @param string $sku
* @return $this
*/
public function setSku($sku)
{
$this->_sku=$sku;
}

[/code]

0

I thought you could do it but I've looking everywhere and I can't seem to see how its done without typing it.

0

OK ... so you have getters/setters and need to introduce fields. Am I correct?

 

If so -- you can do it one by one:

  • Place caret on first unknown field place
  • Invoke Quick Fix menu (Alt+Enter)
  • Use "Add field" intention/fix.

Or you can do the same in a batch mode:

  • Run "Code | Inspect Code..." for current file
  • Select appropriate warnings
  • Apply proposed fix to all of then in one go

Code sample:

<?php

class Foo
{
    public function getSku()
    {
        return $this->_sku;
    }

    /**
     * Set product sku
     *
     * @param string $sku
     * @return $this
     */
    public function setSku($sku)
    {
        $this->_sku = $sku;
    }

    public function getCode()
    {
        return $this->_code;
    }

    /**
     * Set product sku
     *
     * @param string $code
     * @return $this
     */
    public function setCode($code)
    {
        $this->_code = $code;
    }
}

 

0

That's great! Thanks very much.

0

Please sign in to leave a comment.