What are the Slack Archives?
It’s a history of our time together in the Slack Community! There’s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.
Because this space is not active, you won’t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..
🐘
Comments
-
Yeah, we use that e.g. https://github.com/spryker/decimal-object/blob/c4e43b1b62e5b326dad149e9631f2a379ebf1ae4/src/Decimal.php#L152 for more speaking api.
0 -
btw, when we could expect something like:
$foo = FooTransfer::new()->setBar(BarTransfer::new());
…instead of:
$foo = (new FooTransfer())->setBar(new BarTransfer());
?
0 -
well… personally, I don’t see a big difference between these two options. Imho, if the named constructor is not adding value but just a “proxy” to the real constructor, then I wouldn’t advocate using named constructors. I mean, named constructors are great at providing meaning, context, validation, allowing creating an object from different inputs or with different collaborators… TL;DR: I don’t see any benefit for named constructors for DTOs, for example. On the other hand, ValueObjects are a different topic.
0 -
I can understand the "less ()" typing to be handy. You can open a PR and ticket in support for this and we can then discuss and "use" your PR.
One issue to be discussed: "new" vs "create" etc. Naming is hard, and we would need to find a naming here for that proxy method.
In a different context and open source I used that naming for example: https://github.com/dereuromark/cakephp-dto/blob/master/src/Dto/Dto.php#L670 -
I mean, if looking at some other languages,
new
is the most common keyword for naming aconstructor
…In
rust
(https://doc.rust-lang.org/std/keyword.struct.html)new
is used:The most common way to make a new struct is via a constructor method such as
new()
struct Person { name: String, age: u32, } impl Person { fn new(name: String) -> Self { Self { name, 42 } } }
In
go
(https://gobyexample.com/structs) alsonew
is used when creating structures:This
person
struct type hasname
andage
fields.newPerson
constructs a new person struct with the given name.type person struct { name string age int } func newPerson(name string) *person { p := person{name: name} p.age = 42 return &p }
0 -
Real world example from our project:
final class DeliveryName { private const MAX_LENGTH_FIRSTNAME = 36; private string $firstName; private string $lastName; private function __construct(string $firstName, string $lastName) { $this->firstName = $firstName; $this->lastName = $lastName; } public function __toString(): string { if ($this->getLength() > self::MAX_LENGTH_FIRSTNAME) { $this->firstName = mb_substr($this->firstName, 0, self::MAX_LENGTH_FIRSTNAME - $this->getLength()) ?: ''; $this->firstName = trim($this->firstName); } return $this->getFullName(); } public static function fromPayload(array $payload): self { $address = $payload['order']['shipping_address'] ?? []; if (!isset($address['first_name'], $address['last_name'])) { throw new InvalidArgumentException('Missing name in payload'); } return new self($address['first_name'], $address['last_name']); } private function getFullName(): string { return trim(sprintf('%s %s', $this->firstName, $this->lastName)); } private function getLength(): int { return mb_strlen($this->getFullName()); } }
We use that in the middleware and create the object from the payload that is available inside the middleware. This is used to get the name that is printed on the shipping label, it shortens the firstname in case of a too long name, but never touches the lastname. Note that the
__construct
method isprivate
so the only way to create this class is to useDeliveryName::fromPayload
, which makes sure, that we have validation in one central place at the very beginning.0
Categories
- All Categories
- 42 Getting Started & Guidelines
- 7 Getting Started in the Community
- 8 Additional Resources
- 7 Community Ideas and Feedback
- 75 Spryker News
- 920 Developer Corner
- 780 Spryker Development
- 89 Spryker Dev Environment
- 362 Spryker Releases
- 3 Oryx frontend framework
- 34 Propel ORM
- 68 Community Projects
- 3 Community Ideation Board
- 30 Hackathon
- 3 PHP Bridge
- 6 Gacela Project
- 25 Job Opportunities
- 3.2K 📜 Slack Archives
- 116 Academy
- 5 Business Users
- 370 Docker
- 551 Slack General
- 2K Help
- 75 Knowledge Sharing
- 6 Random Stuff
- 4 Code Testing
- 32 Product & Business Questions
- 70 Spryker Safari Questions
- 50 Random