How can I find what class type is expected for a method's property/properties? [duplicate]












0
















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';
}
...
?>









share|improve this question













marked as duplicate by Nigel Ren php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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 '18 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 hinting Foo $foo_instance in your method do prevents from being able to execute no matter the class?

    – Anwar
    Nov 21 '18 at 15:38






  • 1





    What what is worth, you might be interested in Design Strategy Pattern.

    – Anwar
    Nov 21 '18 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 '18 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 '18 at 15:57
















0
















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';
}
...
?>









share|improve this question













marked as duplicate by Nigel Ren php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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 '18 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 hinting Foo $foo_instance in your method do prevents from being able to execute no matter the class?

    – Anwar
    Nov 21 '18 at 15:38






  • 1





    What what is worth, you might be interested in Design Strategy Pattern.

    – Anwar
    Nov 21 '18 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 '18 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 '18 at 15:57














0












0








0









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';
}
...
?>









share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 15:21









Phil TunePhil Tune

2,26431939




2,26431939




marked as duplicate by Nigel Ren php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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 '18 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 php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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 '18 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 hinting Foo $foo_instance in your method do prevents from being able to execute no matter the class?

    – Anwar
    Nov 21 '18 at 15:38






  • 1





    What what is worth, you might be interested in Design Strategy Pattern.

    – Anwar
    Nov 21 '18 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 '18 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 '18 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 '18 at 15:38






  • 1





    What what is worth, you might be interested in Design Strategy Pattern.

    – Anwar
    Nov 21 '18 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 '18 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 '18 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 '18 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 '18 at 15:38




1




1





What what is worth, you might be interested in Design Strategy Pattern.

– Anwar
Nov 21 '18 at 15:39





What what is worth, you might be interested in Design Strategy Pattern.

– Anwar
Nov 21 '18 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 '18 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 '18 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 '18 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 '18 at 15:57












1 Answer
1






active

oldest

votes


















0














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();





share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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();





    share|improve this answer




























      0














      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();





      share|improve this answer


























        0












        0








        0







        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();





        share|improve this answer













        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();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 15:46









        Phil TunePhil Tune

        2,26431939




        2,26431939

















            Popular posts from this blog

            How to send String Array data to Server using php in android

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Is anime1.com a legal site for watching anime?