vscode插件php setter getter怎么配置?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vscode插件php setter getter怎么配置?相关的知识,希望对你有一定的参考价值。
我下了php setter getter插件,但是没有用,无法生成get set方法,会报command 'phpGettersSetters.insertGetterAndSetter' not found错误,我知道是没有配置,但是到底怎么配置啊,在扩展商店那里也没有写。
参考技术A在vscode中,点击左边那排按钮最下面的一个
请点击输入图片描述
这里有个搜索框,可以在里面搜索插件
请点击输入图片描述
例如我们搜索"Visual Studio Code Settings Sync",这是一款能够同步自己vscode设置的插件。
输入搜索词后,左边会展示你的搜索结果,点击其中一个,在右边的界面上点击安装按钮。
请点击输入图片描述
等待数秒安装完毕
请点击输入图片描述
安装完毕后需要点击重新加载才能使它生效
请点击输入图片描述
重启vscode后,右下角的提示就是刚才的插件带来的
请点击输入图片描述
PHP Magic Method Setter and Getter
<?php
/*
Magic method __set() and __get()
1.The definition of a magic function is provided by the programmer – meaning you, as the programmer, will actually write
the definition. This is important to remember – PHP does not provide the definitions of the magic functions
– the programmer must actually write the code that defines what the magic function will do. But, magic functions will
never directly be called by the programmer – actually, PHP will call the function ‘behind the scenes’. This is why they
are called ‘magic’ functions – because they are never directly called, and they allow the programmer to do some pretty
powerful things.
2.PHP functions that start with a double underscore – a “__” – are called magic functions (and/or methods) in PHP.
They are functions that are always defined inside classes, and are not stand-alone (outside of classes) functions.
The magic functions available in PHP are: __construct(), __destruct(), __call(), __callStatic(), __get(), __set(),
__isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone(), and __autoload()
3.magic methods can be defined in such a way that they can be called automatically and does not require any function call
to execute the code inside these functions.
4.These special functions should be defined by the user. But no need to call them explicitly. Rather, it will be called
on appropriate event occurrence. For example, class __construct() will be called while instantiating the class.
PHP magic methods must be defined inside the class.
get more info at https://phppot.com/php/php-magic-methods/
*/
//Example address http://www.learningaboutelectronics.com/Articles/Get-and-set-magic-methods-in-PHP.php
class kids
{
private $age;
protected $height =23;
//setter will be invoked when something outside class access inaccessible variables, for encapsulation
//including protected, private and not declared variable,
// but public variable will not invoke the setter and getter, because we can access public variable
public function __set($property, $value)
{
if ($value > 30)
{
$current =$this->$property;
//notice: $this->property(without $) will create a new attribute called property, $property
$this->$property= $value;
echo "You are going to update $property from $current to $value"."<br>";
}else{
echo "Update $property failed!<br>";
}
}
public function __get($property)
{
return "You are trying to get inaccessible $property 's value: " . $this->$property . "<br>";
}
}
$kid= new kids; //() is optional, automatically call __constructor(){}
//1. testing protected variable
$kid->height =55; //this will implicitly and automatically call __set() because height property is protected
//You are going to update height from 23 to 55
$kid->height =23; //Update height failed!
echo $kid->height; //You are trying to get inaccessible height 's value: 55
//2. testing private variable
$kid->age = 37; //You are going to update age from to 37
echo $kid->age; //You are trying to get inaccessible age 's value: 37
//3.testing undeclared variable in the class
$kid->weight =40;
/*You are going to update weight from You are trying to get inaccessible weight 's value:
to 40 */ //why ?
//because $current=$this->$property =$this->weight statement invoke the getter
echo $kid->weight; //40
以上是关于vscode插件php setter getter怎么配置?的主要内容,如果未能解决你的问题,请参考以下文章
Idea插件之Lombok简化实体类setter/getter操作
是否有任何 Maven 插件可用于从 Jhipster 中删除生成类的 getter/setter 并添加 lombok [关闭]