PHP 8: Enum in class? - php

I have a class that works on only the two types a and b.
My "oldstyle" code today:
class Work1 {
public function do(string $type):string {
if ($type!="a" && $type!="b")
throw new Error("wrong type");
return "type is $type";
}
}
echo (new Work())->do("a"); // type is a
echo (new Work())->do("c"); // exception: wrong type
Now with PHP 8 we have enum and better argument checking options:
enum WorkType {
case A;
case B;
}
class Work2 {
public function __construct(private WorkType $type) {}
public function do() {
return "type is ".$this->type->name;
}
}
echo (new Work2(WorkType::A))->do(); // type is A
Since WorkType and Work2 are unrelated I like to move the Enum WorkType declaration to inside the class Work2. Or does it have to be outside by language design?

You cannot embed the enum within the class; however, in PHP enums are effectively classes, so you can easily merge the class and the enum into one (which is likely what you're looking for)
Consider something like this:
enum Work {
case A;
case B;
public function do() {
return match ($this) {
static::A => 'Doing A',
static::B => 'Doing B',
};
}
}
$x = Work::A;
$x->do();

Related

PHP use ENUM in Attributes

Look at the following code:
<?php
enum Types:string {
case A = 'a';
case B = 'b';
}
#[Attribute(Attribute::TARGET_CLASS)]
class MyAttribute {
public function __construct(public readonly array $mapping)
{
}
}
#[MyAttribute(mapping: [Types::A->value => ''])]
class Entity {
}
It has error Constant expression contains invalid operations. I would like to use Enum value in my attribute for defining configuration. Seem like it is bug in php. Should it be reported or something?
The problem is that when we call Types::A->value it actually creates instance of an enum, which is not a constant value.
To solve this problem define a constant and reference it.
<?php
abstract class Type {
public const A = 'a';
public const B = 'b';
}
enum TypesEnum:string {
case A = Type::A;
case B = Type::B;
}
#[Attribute(Attribute::TARGET_CLASS)]
class MyAttribute {
public function __construct(public readonly array $mapping)
{
}
}
#[MyAttribute(mapping: [Type::A => ''])]
class Entity {
}
Watch out for this issue in php

Return variable class name from php factory

I've got a factory that I want to return a ::class from. However, the factory could potentially return a couple dozen different types (determined by the type of object passed into the factory), named TypeOneObject, TypeTwoObject etc. Is it possible to return the class using a variable, something like this?
$type = $myrequiredclass->getType();
return $type."Object"::class; // wanting TypeOneObject::class
It seems like no matter how I construct this return statement I always get PHP Parse error: syntax error, unexpected '::'
I know it'd be easy enough to do with a big if/then or switch but I'd like to avoid that.
Here's a more fleshed out scenario:
class TypeOneObject
{
public static function whoAreYou()
{
return 'Type One Object!';
}
}
class MyRequiredClass
{
public function getType()
{
return 'TypeOne';
}
}
class MyFactory
{
public static function getFactoryObject(MyRequiredClass $class)
{
$type = $class->getType()."Object";
return $type::class;
}
}
$object = MyFactory::getFactoryObject(new MyRequiredClass());
$object::whoAreYou();
The best way to get the class name from the $type instance is to use php get_class_methods function. This will get us all the methods inside the class instance. from there we can filter and use call_user_func to call the method and get the right values.
class TypeOneObject
{
public static function whoAreYou()
{
return 'Type One Object!';
}
}
class MyRequiredClass
{
public function getType()
{
return 'TypeOne';
}
}
class MyFactory
{
public static function getFactoryObject(MyRequiredClass $class)
{
$methods = get_class_methods($class);
$method = array_filter($methods, function($method) {
return $method == 'getType';
});
$class = new $class();
$method = $method[0];
$methodName = call_user_func([$class, $method]);
$objectName = sprintf('%sObject', $methodName);
return new $objectName;
}
}
$object = MyFactory::getFactoryObject(new MyRequiredClass());
echo $object::whoAreYou();
Output
Type One Object!

How can I achieve something like $app->error()->encode() in PHP?

I want to include this function in a class. This function will essentially json_encode the output of the previous function (I hope that makes sense).
I want to do something like this:
<?php
$app = new App;
// $app->error(); // Should return ['some error', 'some other error']
echo $app->error()->encode(); // Should return {'errors': ['some error', 'some other error']}.
Also what's the correct term for such function? I've been searching but couldn't find anything as I didn't really know what I was looking for.
Thanks!
Edit
I think you got that wrong. It's my mistake I didn't mention it before.
That's just an example. Just like in frameworks like Slim, where you can do something like:
$response->getBody()->write('Something');
I want to do something similar to that. Not just that. I want to learn how it's done.
Here is some boilerplate code you could use. The idea is that you should make the error method return an object of yet another class. That object should then in turn have the encode method.
In your example, you want $app->error() to return an array. For it to behave as an array, we can extend the ArrayObject class.
Secondly, you want that same $app->error() to expose another method encode. So you define that method in that same class mentioned above:
// Extend ArrayObject to make objects behave as arrays
class ErrorMsg extends ArrayObject {
// Add method to return JSON string
public function encode() {
return json_encode(array("errors" => $this->getArrayCopy()));
}
}
class App {
private $error;
public function doSomething() {
// For demo sake, just set an error:
$this->error = ["An error occurred in doSomething", "No further info"];
}
public function error() {
// This is the key: instantiate and return another object
return new ErrorMsg($this->error);
}
}
$app = new App;
// Set an error condition in $app
$app->doSomething();
// Loop over error array
foreach ($app->error() as $index => $error) {
// Display the error
echo "Error $index is: $error\n";
}
// Display the JSON encoding of the same.
echo $app->error()->encode() . "\n";
Output:
Error 0 is: An error occurred in doSomething
Error 1 is: No further info
{"errors":["An error occurred in doSomething","No further info"]}
See it run on eval.in
General idea to chain method calls
In general, when you want your objects to support chained -> notation, you must make sure to define each method as returning yet another object with its own methods. Then those methods can again return objects, with again exposed methods, etc. And so you can chain-call on and on.
So if you want to be able to write:
$a = new A();
$result = $a->b()->c()->d();
...then your code would look something like this:
class D {
// ...
}
class C {
private $d;
public function C() { // constructor
$this->d = new D();
}
public function d() {
return $this->d;
}
}
class B {
private $c;
public function B() { // constructor
$this->c = new C();
}
public function c() {
return $this->c;
}
}
class A {
private $b;
public function A() { // constructor
$this->b = new B();
}
public function b() {
return $this->b;
}
}
Of course, this is just the structure, and you'd pass some specific data to either the constructors and/or methods. But the main idea is that you never return simple types (String, Number, ...), but always objects.

Casting Objects with the same Superclass

Given:
class X extends A
class Y extends A
I have an Object of Class X and want to create an Object of Class Y that shares all fields with the Object of Class X (no matter if deep or shallow copy) that they have in common through Class A.
Can I cast Objects of Class X to Class Y or somehow else copy all attributes they have in common?
If you would have simply asked: how can I easily create a variable of class Y that has all the common contents of class A of a variable of class X? Then I think you would have gotten a quicker answer. However you asked a question about casting.
Casting is a way of 'tricking' the compiler (or in this case the interpreter) to interpret a variable of one type as a variable of another type. If you know what you are doing this can come in handy sometimes. Most of the time however, it is a bad idea.
Casting to objects is not possible in PHP, but if it were and you could write:
class A {
public $foo = 1;
}
class X extends A {
public $bar = "2";
}
class Y extends A {
public $bom = 3;
}
$x = new X();
$y = (Y) x;
Would you want PHP to think variable $y is of type Y or would you want it to actually be of type Y? The above would achieve the first, not the latter. As soon as you would access $y->bom your program's execution would run into a problem, since no memory was allocated for $bom nor was it initialized. In languages that do allow t his, like c++, this would likely cause a GPF.
Therefore you don't need to cast, you don't want to cast and you can't cast to begin with. Forget casting :)
What you want to do is create some method that copies the fields of another object to your object instance. Here is an example:
class A {
public $foo;
public function copy($cA) {
$this->foo = $cA->foo;
}
}
class X extends A {
public $bar;
public function copy($cX) {
if ($cX instanceof A) parent::copy($cX);
if ($cX instanceof X) {
$this->bar = $cX->bar;
}
}
}
class Y extends A {
public $bom;
public function copy($cY) {
if ($cY instanceof A) parent::copy($cY);
if ($cY instanceof Y) {
$this->bom = $cY->bom;
}
}
}
$x = new X();
$x->foo = "hello";
$x->bar = "world";
$otherX = new X();
$otherX->copy($x);
echo "$otherX->foo $otherX->bar \n"; // hello world
$y = new Y();
$y->copy($otherX);
echo "$y->foo \n"; // hello
The method of copying in this example is by assignment. This is not very flexible though, since this means everytime you add a property or rename a property in one of these classes you'd have to update the copy method to reflect the change.
Here is a more flexible way of achieving the same thing:
class A {
protected $foo;
public function copy($cA) {
foreach (get_object_vars($cA) as $key => $value) {
$this->$key = $value;
}
}
public function setFoo($foo) {
$this->foo = $foo;
}
}
class X extends A {
public $bar;
public function out() {
echo "$this->foo $this->bar \n";
}
}
class Y extends A {
public $bom;
public function out() {
echo "$this->foo $this->bom \n";
}
}
// setup x
$x = new X();
$x->setFoo("hello");
$x->bar = "world";
// setup otherX
$otherX = new X();
$otherX->copy($x);
$x->setFoo("hi");
$otherX->out(); // -> hello world
// setup Y
$y = new Y();
$y->copy($x);
$y->bom = "php";
$y->out(); // -> hi php
Though this method is a lot more flexible it is also somewhat crude.
echo $y->bar; // has become "world"
This happens because get_object_vars doesn't look at what variables each class type can have. You could work around this to some extent using ReflectionClass, but I am guessing by this time you've gotten the answer you needed.
Hope this helps.
Good luck!

PHP dynamic class extending

I know you can extend a class when constructing it like the following:
class b extends a {
}
But is it possible to dynamically extend classes from the scripts? Such as:
$b = new b($input) extends a;
What I wish to accomplish is to extend the module differnetly wheither it's used in admin rather than the public pages. I know I can create two different parent classes by the same name and only include one per admin or public. But my question is, is it possible to do it dynamically in PHP?
No, not without an extension like RunKit.
You might consider an alternative approach. If you want B to assume the functionality of A, perhaps something like the following could provide a sort of "mixin" approach. The general picture is that, instead of B being a child of A, B delegates to A.
<?php
class MixMeIn
{
public $favouriteNumber = 7;
public function sayHi() {
echo "Hello\n";
}
}
class BoringClass
{
private $mixins = array();
public function mixin($object)
{
$this->mixins[] = $object;
}
public function doNothing() {
echo "Zzz\n";
}
public function __call($method, $args)
{
foreach ($this->mixins as $mixin)
{
if (method_exists($mixin, $method))
{
return call_user_func_array(array($mixin, $method), $args);
}
}
throw new Exception(__CLASS__ + " has no method " + $method);
}
public function __get($attr)
{
foreach ($this->mixins as $mixin)
{
if (property_exists($mixin, $attr))
{
return $mixin->$attr;
}
}
throw new Exception(__CLASS__ + " has no property " + $attr);
}
public function __set($attr, $value)
{
foreach ($this->mixins as $mixin)
{
if (property_exists($mixin, $attr))
{
return $mixin->$attr = $value;
}
}
throw new Exception(__CLASS__ + " has no property " + $attr);
}
}
// testing
$boring = new BoringClass();
$boring->doNothing();
try {
$boring->sayHi(); // not available :-(
}
catch (Exception $e) {
echo "sayHi didn't work: ", $e->getMessage(), "\n";
}
// now we mixin the fun stuff!
$boring->mixin(new MixMeIn());
$boring->sayHi(); // works! :-)
echo $boring->favouriteNumber;
Just a zany idea. I hope I understood the question correctly.
You can't, but this has been requested for a few years: https://bugs.php.net/bug.php?id=41856&edit=1
You can define the classes within an eval, but it's more trouble than declaring the class normally.
But you cannot use extends while object creation. extends is used in class definition only and defines which other class is "parent" for our new class.
Alternatively, if you are comfortable with javascript-style inheritance and don't mind losing typechecking:
<? //PHP 5.4+
final class ExpandoLookalike {
//Allow callable properties to be executed
public function __call($name, $arguments) {
\call_user_func_array($this->$name, $arguments);
}
}
$newBaseModule = static function(){
$base = new ExpandoLookalike();
//Common base functions get assigned here.
$basePrivateVar = 42;
$base->commonFunction = static function($params1, $params2) use ($basePrivateVar){
echo "common function\n";
};
$base->comment = static function() use ($basePrivateVar){
echo "Doing base comment with $basePrivateVar\n";
};
return $base;
};
//Javascript-style extends
$newAdminModule = static function($param) use ($newBaseModule){
$base = $newBaseModule();
$privateVar = 5;
$base->adminProperty = 60;
$base->suspendSite = static function() use ($param, $privateVar){
echo 'Doing admin only function ';
echo "with $param, $privateVar\n";
};
return $base;
};
$newPublicModule = static function() use ($newBaseModule){
$base = $newBaseModule();
$privateVar = 3;
//Javascript-style overloading
$oldComment = $base->comment;
$base->comment = static function($data) use ($oldComment, $privateVar){
$oldComment();
echo 'Doing public function ';
echo "with $data\n";
};
return $base;
};
$baseModule = $newBaseModule();
$adminModule = $newAdminModule('P');
$publicModule = $newPublicModule();
$adminModule->suspendSite(); //echos 'Doing admin only function with P, 5'
echo "{$adminModule->adminProperty}\n"; //echos '60'
$publicModule->comment('com'); //echos 'Doing base comment with 42'
//'Doing public function with com'
?>
Despite closing the door to traits and interfaces, it opens up other interesting doors to compensate:
<? //PHP 5.4+
$inheritAllTheThings = static function(){
$base = new ExpandoLookalike();
foreach(\func_get_args() as $object){
foreach($object as $key => $value){
//Properties from later objects overwrite properties from earlier ones.
$base->$key = $value;
}
}
return $base;
};
$allOfEm = $inheritAllTheThings(
$newPublicModule(),
$newAdminModule('Q'),
['anotherProp' => 69,]
);
$allOfEm->comment('f'); //echos 'Doing base comment with 42'
//Because AdminModule came after PublicModule, the function that echos 'f'
//from PublicModule was overridden by the function from AdminModule.
//Hence, order denotes resolutions for multiple inheritance collisions.
$allOfEm->suspendSite(); //echos 'Doing admin only function with Q, 5'
echo $allOfEm->anotherProp . "\n"; //echos '69'
?>
You can with typecasting. If a extends b then you could do
$a=(a)(new b($input));
Which isn't exactly the same, but similar.
You can look: https://github.com/ptrofimov/jslikeobject
Author implemented JS-like objects with support of inheritance.
But perhaps it is not so good to use such objects instead of usual ones.
Yes, as cory mentioned, this feature has been requested before. But before that, you can create a workaround. Here is my old school trick for this
Create two separate classes like these:
class a {
}
class b {
public $object;
}
Then, create an extended version too
class bextendeda extends a {
}
In the constructor method of class b, place few functions which redirects to the extended object if requested.
class b {
public $object;
public function __contruct($extend = false) {
if($extend) $this -> object = new bextendeda();
else $this -> object = $this;
}
function __get($prop) {
return $this-> object -> $prop;
}
function __set($prop, $val) {
$this-> object -> $prop = $val;
}
function __call($name, $arguments)
{
return call_user_func_array(array($this -> object, $name), $arguments);
}
}
And there you have it, IF you want the extended version just do this
$b = new b(true);
If not
$b = new b();
Enjoy :)

Categories