Say, I have a class with consts like so,
class Car
{
// Types
const TYPE_SALOON = "Saloon";
const TYPE_HATCHBACK = "Hatchback";
...
public static function validTypes() {
return array(
static::TYPE_SALOON,
static::TYPE_HATCHBACK,
...
);
}
}
I need the constructor to accept an input argument called type, which exits in the array returned by validTypes(). The way I'd normally do this is:
function __construct($type, ...) {
if(!in_array($type, static::validTypes())) {
// Throw Exception
}
}
What's a more elegant way to do this?
Is there a way to do this with typeHinting?
Related
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!
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.
I am new to PHP and encountered some odd behavior that may or may not be a bug in my version of PHP (5.4.13). I found a case where the sequence of function arguments matters in the function declaration. The in the following example:
class Book {
function __construct() { }
public static function withDescriptors($isNotOrmObject, $isArray, $isScalar) {
$instance = new self();
// initialization here
return $instance;
}
}
Invoking withDescriptors causes an 'array to string conversion' exception. The error is thrown when withDescriptors is invoked, ie. withDescriptors is never actually executed. However, switching the object parameter with the array parameter solves the problem. I.e.
public static function withDescriptors($isArray, $isNotOrmObject, $isScalar){ ....
Is this a known characteristic of PHP or is this a bug?
More explicitly:
class Book {
function __construct() { }
public static function withDescriptors($isNotOrmObject, $isArray, $isScalar) {
$instance = new self();
// initialization here
return $instance;
}
}
$book = Book::withDescriptors($isNotORMobject, $isArray, $isScalar);
FAILS and
class Book {
function __construct() { }
public static function withDescriptors($isArray, $isNotORMobject, $isScalar) {
$instance = new self();
// initialization here
return $instance;
}
}
$book = Book::withDescriptors($isArray, $isNotORMobject, $isScalar);
WORKS great. The ONLY difference is the parameter sequence, the "initialization here" code is identical.
The reason you don't get the warning in your second example, is that the Object you're passing as the second parameter is implementing a magic __toString() method. PHP is not a strong typed language, but more recent versions have limited capabilities of type hinting.
To illustrate the warning;
function saySomething($shouldBeAString)
{
echo $shouldBeAString
}
saySomething(array('hello'));
Outputs
'Array'
and a warning array to string conversion
class Foo {
public function __toString() {
return 'Hi I am Foo';
}
}
$bar = new Foo();
saySomething($bar);
Will output
'Hi I am Foo'
Without warning
As mentioned, PHP offers limited type hinting. You can specify a required 'class/object type' and 'array' as accepted arguments, but not scalar types, like 'string', 'int', 'bool' etc.
function (array $myarray) {
// will only accept arrays
// or something that implements array-access
}
function (Foo $var) {
// Will only accept 'Foo' objects
}
Polymorphism / Function overloading
Some other languages allow defining the same method/function multiple times, but with a different signature (other argument types). PHP does not explicitly support this.
For example, in other languages, this is allowed:
function doSomething(string $arg1) { ......}
function doSomething(array $arg1) { .... }
function doSomething(string $arg1, string $arg2) { ... }
In those languages, depending on the type and number of arguments, variant 1, 2 or 3 will be executed. PHP does not support this as it requires functions/methods to have a unique name. PHP will therefore complain that function doSomething() is already defined.
However, you can create something similar in PHP in several ways;
// rename the functions/methods so that they have a unique name
function _doSomething1(string $arg1) { ......}
function _doSomething2(array $arg1) { .... }
function _doSomething3(string $arg1, string $arg2) { ... }
// create the 'wrapper' function *without arguments specified*
function doSomething() {
// determin which variant should be executed
$numargs = func_num_args();
$args = func_get_args();
if ($numargs == 2) {
// 2 arguments -> variant 3
return call_user_func_array('_doSomething3', $args);
} else if {$numargs == 1) {
if (is_array($args[0]) {
// first argument is an array
return call_user_func_array('_doSomething2', $args);
} else {
return call_user_func_array('_doSomething1', $args);
}
}
}
Note: code above is 'fake' code, just to illustrate the idea!
In general, order matters in function arguments.
The only time it doesn't is when arguments are the same type or your function implements something like get opt long.
There are some good answers here but this particular situation arose from a bug in the PHP interpreter.
I want to have a dictionary of functions. With this dictionary, I could have a handler that accepts a function name and an array of arguments, and executes that function, returning the value it returns if it returns anything. The handler would throw an error if the name does not correspond to an existing function.
This would be very simple to implement Javascript:
var actions = {
doSomething: function(){ /* ... */ },
doAnotherThing: function() { /* ... */ }
};
function runAction (name, args) {
if(typeof actions[name] !== "function") throw "Unrecognized function.";
return actions[name].apply(null, args);
}
But since functions aren't really first class objects in PHP, I can't figure out how to do this easily. Is there a reasonably simple way to do this in PHP?
$actions = array(
'doSomething' => 'foobar',
'doAnotherThing' => array($obj, 'method'),
'doSomethingElse' => function ($arg) { ... },
...
);
if (!is_callable($actions[$name])) {
throw new Tantrum;
}
echo call_user_func_array($actions[$name], array($param1, $param2));
Your dictionary can consist of any of the allowable callable types.
I don't clearly get what you mean.
If you need an array of functions just do:
$actions = array(
'doSomething'=>function(){},
'doSomething2'=>function(){}
);
You can than run a function with $actions['doSomething']();
Of course you can have args:
$actions = array(
'doSomething'=>function($arg1){}
);
$actions['doSomething']('value1');
You could use PHP's __call() for that:
class Dictionary {
static protected $actions = NULL;
function __call($action, $args)
{
if (!isset(self::$actions))
self::$actions = array(
'foo'=>function(){ /* ... */ },
'bar'=>function(){ /* ... */ }
);
if (array_key_exists($action, self::$actions))
return call_user_func_array(self::$actions[$action], $args);
// throw Exception
}
}
// Allows for:
$dict = new Dictionary();
$dict->foo(1,2,3);
For static invocations, __callStatic() can be used (as of PHP5.3).
If You plan to use this in object context You do not have to create any function/method dictionary.
You can simply raise some error on unexisting method with magic method __call():
class MyObject {
function __call($name, $params) {
throw new Exception('Calling object method '.__CLASS__.'::'.$name.' that is not implemented');
}
function __callStatic($name, $params) { // as of PHP 5.3. <
throw new Exception('Calling object static method '.__CLASS__.'::'.$name.' that is not implemented');
}
}
Then every other class should extend Your MyObject class...
http://php.net/__call
// >= PHP 5.3.0
$arrActions=array(
"doSomething"=>function(){ /* ... */ },
"doAnotherThing"=>function(){ /* ... */ }
);
$arrActions["doSomething"]();
// http://www.php.net/manual/en/functions.anonymous.php
// < PHP 5.3.0
class Actions{
private function __construct(){
}
public static function doSomething(){
}
public static function doAnotherThing(){
}
}
Actions::doSomething();
http://php.net/manual/en/function.call-user-func.php
call_user_func will let you execute your functions from their names as a string and pass them parameters, I don't know the performance implications of doing it this way though.
I'm aware of the existence of call_user_func_array, but I don't believe you can use this to construct a class (if you can, how do you do it?). As long as I'm not mistaken, how do you instantiate a class with an array as parameters?
for example:
class Test {
public function __construct($var1, $var2) {
// do something
}
}
how would I instantiate it with this:
array("var1_data", "var2_data")
class Test {
public function __construct(array $params) {
// ...
}
}
Don't use “magic” unless you really need it.
EDIT:
If what you need is varagrs, you can find an answer here.
If you must have multiple constructors, you should name them something other than __construct, then define a __construct method that can accept any number of arguments. Within that method, you can determine which of your custom constructors to use. If all you want to do is allow the constructor to be passed an array instead of a list of arguments, you can do it this way (note that this example lacks any error checking):
public function __construct() {
$args = func_get_args();
if(count($args) == 1 && is_array($args[0])) {
$cArgs = $args[0];
} else {
$cArgs = $args;
}
__do_construct($cArgs[0], $cArgs[1]);
}
private function __do_construct($arg1, $arg2) {
// do something
}