How can I find what class type is expected for a method's property/properties? [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
PHP Reflection - Get Method Parameter Type As String
6 answers
I need to see what class type is being expected (type-hinted) for a method property.
<?php
class Foo {}
class Bar {
public function do(Foo $foo_instance) {}
}
$bar = new Bar();
$some_instance = new ??();
$bar->do($some_instance);
?>
I think this is something available in the Reflection API, but I have yet to find anything that spits out 'Foo'
as my type-hint for Bar::do
. Any ideas?
Context
I want to do something like:
<?php
...
if ( myMethodExpects($class, $method, 'Foo') ) {
$some_instance = new Foo();
} elseif ( myMethodExpects($class, $method, 'Baz') {
$some_instance = new Baz();
} elseif ( myMethodHasNoTypeHint($class, $method) ) {
$some_instance = 'just a string';
}
...
?>
php reflection type-hinting
marked as duplicate by Nigel Ren
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 15:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
This question already has an answer here:
PHP Reflection - Get Method Parameter Type As String
6 answers
I need to see what class type is being expected (type-hinted) for a method property.
<?php
class Foo {}
class Bar {
public function do(Foo $foo_instance) {}
}
$bar = new Bar();
$some_instance = new ??();
$bar->do($some_instance);
?>
I think this is something available in the Reflection API, but I have yet to find anything that spits out 'Foo'
as my type-hint for Bar::do
. Any ideas?
Context
I want to do something like:
<?php
...
if ( myMethodExpects($class, $method, 'Foo') ) {
$some_instance = new Foo();
} elseif ( myMethodExpects($class, $method, 'Baz') {
$some_instance = new Baz();
} elseif ( myMethodHasNoTypeHint($class, $method) ) {
$some_instance = 'just a string';
}
...
?>
php reflection type-hinting
marked as duplicate by Nigel Ren
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 15:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Does type hintingFoo $foo_instance
in your methoddo
prevents from being able to execute no matter the class?
– Anwar
Nov 21 at 15:38
1
What what is worth, you might be interested in Design Strategy Pattern.
– Anwar
Nov 21 at 15:39
@Anwar awesome. I have some downtime between jobs and trying to level up my PHP skillz by building various app components.
– Phil Tune
Nov 21 at 15:54
And yeah, PHP will throw an exception if I put in the wrong instance or type of data. I want to do something like "if method expects some model instance, give it that, otherwise just pass the(int)$id
or whatever"
– Phil Tune
Nov 21 at 15:57
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
PHP Reflection - Get Method Parameter Type As String
6 answers
I need to see what class type is being expected (type-hinted) for a method property.
<?php
class Foo {}
class Bar {
public function do(Foo $foo_instance) {}
}
$bar = new Bar();
$some_instance = new ??();
$bar->do($some_instance);
?>
I think this is something available in the Reflection API, but I have yet to find anything that spits out 'Foo'
as my type-hint for Bar::do
. Any ideas?
Context
I want to do something like:
<?php
...
if ( myMethodExpects($class, $method, 'Foo') ) {
$some_instance = new Foo();
} elseif ( myMethodExpects($class, $method, 'Baz') {
$some_instance = new Baz();
} elseif ( myMethodHasNoTypeHint($class, $method) ) {
$some_instance = 'just a string';
}
...
?>
php reflection type-hinting
This question already has an answer here:
PHP Reflection - Get Method Parameter Type As String
6 answers
I need to see what class type is being expected (type-hinted) for a method property.
<?php
class Foo {}
class Bar {
public function do(Foo $foo_instance) {}
}
$bar = new Bar();
$some_instance = new ??();
$bar->do($some_instance);
?>
I think this is something available in the Reflection API, but I have yet to find anything that spits out 'Foo'
as my type-hint for Bar::do
. Any ideas?
Context
I want to do something like:
<?php
...
if ( myMethodExpects($class, $method, 'Foo') ) {
$some_instance = new Foo();
} elseif ( myMethodExpects($class, $method, 'Baz') {
$some_instance = new Baz();
} elseif ( myMethodHasNoTypeHint($class, $method) ) {
$some_instance = 'just a string';
}
...
?>
This question already has an answer here:
PHP Reflection - Get Method Parameter Type As String
6 answers
php reflection type-hinting
php reflection type-hinting
asked Nov 21 at 15:21
Phil Tune
2,21031739
2,21031739
marked as duplicate by Nigel Ren
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 15:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Nigel Ren
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 15:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Does type hintingFoo $foo_instance
in your methoddo
prevents from being able to execute no matter the class?
– Anwar
Nov 21 at 15:38
1
What what is worth, you might be interested in Design Strategy Pattern.
– Anwar
Nov 21 at 15:39
@Anwar awesome. I have some downtime between jobs and trying to level up my PHP skillz by building various app components.
– Phil Tune
Nov 21 at 15:54
And yeah, PHP will throw an exception if I put in the wrong instance or type of data. I want to do something like "if method expects some model instance, give it that, otherwise just pass the(int)$id
or whatever"
– Phil Tune
Nov 21 at 15:57
add a comment |
Does type hintingFoo $foo_instance
in your methoddo
prevents from being able to execute no matter the class?
– Anwar
Nov 21 at 15:38
1
What what is worth, you might be interested in Design Strategy Pattern.
– Anwar
Nov 21 at 15:39
@Anwar awesome. I have some downtime between jobs and trying to level up my PHP skillz by building various app components.
– Phil Tune
Nov 21 at 15:54
And yeah, PHP will throw an exception if I put in the wrong instance or type of data. I want to do something like "if method expects some model instance, give it that, otherwise just pass the(int)$id
or whatever"
– Phil Tune
Nov 21 at 15:57
Does type hinting
Foo $foo_instance
in your method do
prevents from being able to execute no matter the class?– Anwar
Nov 21 at 15:38
Does type hinting
Foo $foo_instance
in your method do
prevents from being able to execute no matter the class?– Anwar
Nov 21 at 15:38
1
1
What what is worth, you might be interested in Design Strategy Pattern.
– Anwar
Nov 21 at 15:39
What what is worth, you might be interested in Design Strategy Pattern.
– Anwar
Nov 21 at 15:39
@Anwar awesome. I have some downtime between jobs and trying to level up my PHP skillz by building various app components.
– Phil Tune
Nov 21 at 15:54
@Anwar awesome. I have some downtime between jobs and trying to level up my PHP skillz by building various app components.
– Phil Tune
Nov 21 at 15:54
And yeah, PHP will throw an exception if I put in the wrong instance or type of data. I want to do something like "if method expects some model instance, give it that, otherwise just pass the
(int)$id
or whatever"– Phil Tune
Nov 21 at 15:57
And yeah, PHP will throw an exception if I put in the wrong instance or type of data. I want to do something like "if method expects some model instance, give it that, otherwise just pass the
(int)$id
or whatever"– Phil Tune
Nov 21 at 15:57
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Okay, asked Google the right questions.
I was looking for ReflectionParameter::getClass. Used like so:
<?php
class Foo {
public function do(Bar $bar, Baz $baz, $foo='') {}
}
$method = new ReflectionMethod('Foo', 'do');
$method_params = $method->getParameters();
foreach ( $method_params as $param ) {
var_dump($param->getClass());
}
?>
/* RETURNS
-> object(ReflectionClass)[6]
public 'name' => string 'Bar' (length=3)
-> object(ReflectionClass)[6]
public 'name' => string 'Baz' (length=4)
-> null
*/
This can also be used on ReflectionFunction
:
$function_params = (new ReflectionFunction('func_name')->getParameters();
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Okay, asked Google the right questions.
I was looking for ReflectionParameter::getClass. Used like so:
<?php
class Foo {
public function do(Bar $bar, Baz $baz, $foo='') {}
}
$method = new ReflectionMethod('Foo', 'do');
$method_params = $method->getParameters();
foreach ( $method_params as $param ) {
var_dump($param->getClass());
}
?>
/* RETURNS
-> object(ReflectionClass)[6]
public 'name' => string 'Bar' (length=3)
-> object(ReflectionClass)[6]
public 'name' => string 'Baz' (length=4)
-> null
*/
This can also be used on ReflectionFunction
:
$function_params = (new ReflectionFunction('func_name')->getParameters();
add a comment |
up vote
0
down vote
Okay, asked Google the right questions.
I was looking for ReflectionParameter::getClass. Used like so:
<?php
class Foo {
public function do(Bar $bar, Baz $baz, $foo='') {}
}
$method = new ReflectionMethod('Foo', 'do');
$method_params = $method->getParameters();
foreach ( $method_params as $param ) {
var_dump($param->getClass());
}
?>
/* RETURNS
-> object(ReflectionClass)[6]
public 'name' => string 'Bar' (length=3)
-> object(ReflectionClass)[6]
public 'name' => string 'Baz' (length=4)
-> null
*/
This can also be used on ReflectionFunction
:
$function_params = (new ReflectionFunction('func_name')->getParameters();
add a comment |
up vote
0
down vote
up vote
0
down vote
Okay, asked Google the right questions.
I was looking for ReflectionParameter::getClass. Used like so:
<?php
class Foo {
public function do(Bar $bar, Baz $baz, $foo='') {}
}
$method = new ReflectionMethod('Foo', 'do');
$method_params = $method->getParameters();
foreach ( $method_params as $param ) {
var_dump($param->getClass());
}
?>
/* RETURNS
-> object(ReflectionClass)[6]
public 'name' => string 'Bar' (length=3)
-> object(ReflectionClass)[6]
public 'name' => string 'Baz' (length=4)
-> null
*/
This can also be used on ReflectionFunction
:
$function_params = (new ReflectionFunction('func_name')->getParameters();
Okay, asked Google the right questions.
I was looking for ReflectionParameter::getClass. Used like so:
<?php
class Foo {
public function do(Bar $bar, Baz $baz, $foo='') {}
}
$method = new ReflectionMethod('Foo', 'do');
$method_params = $method->getParameters();
foreach ( $method_params as $param ) {
var_dump($param->getClass());
}
?>
/* RETURNS
-> object(ReflectionClass)[6]
public 'name' => string 'Bar' (length=3)
-> object(ReflectionClass)[6]
public 'name' => string 'Baz' (length=4)
-> null
*/
This can also be used on ReflectionFunction
:
$function_params = (new ReflectionFunction('func_name')->getParameters();
answered Nov 21 at 15:46
Phil Tune
2,21031739
2,21031739
add a comment |
add a comment |
Does type hinting
Foo $foo_instance
in your methoddo
prevents from being able to execute no matter the class?– Anwar
Nov 21 at 15:38
1
What what is worth, you might be interested in Design Strategy Pattern.
– Anwar
Nov 21 at 15:39
@Anwar awesome. I have some downtime between jobs and trying to level up my PHP skillz by building various app components.
– Phil Tune
Nov 21 at 15:54
And yeah, PHP will throw an exception if I put in the wrong instance or type of data. I want to do something like "if method expects some model instance, give it that, otherwise just pass the
(int)$id
or whatever"– Phil Tune
Nov 21 at 15:57