Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Last active August 18, 2020 20:02
Show Gist options
  • Save mikeschinkel/4c9cc5ed25d6e7665c0e9c70b3458fc8 to your computer and use it in GitHub Desktop.
Save mikeschinkel/4c9cc5ed25d6e7665c0e9c70b3458fc8 to your computer and use it in GitHub Desktop.
Hypothetical example of using use statement for attributes
<?php
namespace MyNamespace;
// namespace use
use WpAttributes\PrimaryKey;
use WpAttributes\PostType;
use WpAttributes\TemplateVariable;
use PHP\VirtualReadonly;
// attribute use
use PostType('abc_product'),
TemplateVariable('product'),
VirtualReadonly('id'),
VirtualReadonly('name');
class Product {
private $id;
private $name;
public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
// attribute use
use PrimaryKey();
public function getId(): string {
return $this->id;
}
public function getName(): string {
return $this->name;
}
public function setName(string $name) {
$this->name = $name;
}
// attribute use
public function __get(string $name) {
switch ($name){
case 'id':
return $this->id;
case 'name':
return $this->name;
}
trigger_error("invalid property '%s'", $name);
return null;
}
}
<?php
namespace MyNamespace;
// namespace use
use WpAttributes\PrimaryKey;
use WpAttributes\PostType;
use WpAttributes\TemplateVariable;
use PHP\VirtualReadonly;
// class attribute use
use attribute PostType('abc_product'),
TemplateVariable('product'),
VirtualReadonly('id'),
VirtualReadonly('name');
class Product {
private $id;
private $name;
public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
// method attribute use
use attribute PrimaryKey();
public function getId(): string {
return $this->id;
}
public function getName(): string {
return $this->name;
}
public function setName(string $name) {
$this->name = $name;
}
// attribute use
public function __get(string $name) {
switch ($name){
case 'id':
return $this->id;
case 'name':
return $this->name;
}
trigger_error("invalid property '%s'", $name);
return null;
}
}
<?php
namespace MyNamespace;
// namespace use
use WpAttributes\PrimaryKey;
use WpAttributes\PostType;
use WpAttributes\TemplateVariable;
use PHP\VirtualReadonly;
@@PostType('abc_product');
@@TemplateVariable('product');
@@VirtualReadonly('id');
@@VirtualReadonly('name');
class Product3 {
private $id;
private $name;
public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
// attribute use
@@PrimaryKey();
public function getId(): string {
return $this->id;
}
public function getName(): string {
return $this->name;
}
public function setName(string $name) {
$this->name = $name;
}
// attribute use
public function __get(string $name) {
switch ($name){
case 'id':
return $this->id;
case 'name':
return $this->name;
}
trigger_error("invalid property '%s'", $name);
return null;
}
}
<?php
namespace MyNamespace;
// namespace use
use WpAttributes\PrimaryKey;
use WpAttributes\PostType;
use WpAttributes\TemplateVariable;
use PHP\VirtualReadonly;
@[PostType('abc_product'),
TemplateVariable('product'),
VirtualReadonly('id'),
VirtualReadonly('name')]
class Product3 {
private $id;
private $name;
public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
// attribute use
@[PrimaryKey]
public function getId(): string {
return $this->id;
}
public function getName(): string {
return $this->name;
}
public function setName(string $name) {
$this->name = $name;
}
// attribute use
public function __get(string $name) {
switch ($name){
case 'id':
return $this->id;
case 'name':
return $this->name;
}
trigger_error("invalid property '%s'", $name);
return null;
}
}
<?php
namespace MyNamespace;
// namespace use
use WpAttributes\PrimaryKey;
use WpAttributes\PostType;
use WpAttributes\TemplateVariable;
use PHP\VirtualReadonly;
#[PostType('abc_product'),
TemplateVariable('product'),
VirtualReadonly('id'),
VirtualReadonly('name')]
class Product3 {
private $id;
private $name;
public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
// attribute use
#[PrimaryKey]
public function getId(): string {
return $this->id;
}
public function getName(): string {
return $this->name;
}
public function setName(string $name) {
$this->name = $name;
}
// attribute use
public function __get(string $name) {
switch ($name){
case 'id':
return $this->id;
case 'name':
return $this->name;
}
trigger_error("invalid property '%s'", $name);
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment