php 将类作为函数参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32614696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:04:46  来源:igfitidea点击:

Passing a class as function parameter

phplaravellaravel-5laravel-5.1

提问by Alex

I'm trying to do something like this:

我正在尝试做这样的事情:

function doSomething($param, Class) {
Class::someFunction();
}

$someVar = doSomething($param, Class);

Is it possible?

是否可以?

To explain better what I'm trying to do. I have a helper function in Laravel to generate unique slugs, so I have to query different tables depending on where the slug is going to be saved.

为了更好地解释我正在尝试做的事情。我在 Laravel 中有一个辅助函数来生成唯一的 slug,所以我必须根据要保存 slug 的位置查询不同的表。

Actual code I'm trying to write:

我正在尝试编写的实际代码:

$newcat->slug = $helper->uniqueSlug($appname, Apk);

public function uniqueSlug($str, Apk)
    {
        $slug = Str::slug($str);

        $count = Apk::whereRaw("slug RLIKE '^{$slug}(-[0-9]+)?$'")->count();

        return $count ? "{$slug}-{$count}" : $slug;
    }

Thanks!

谢谢!

回答by Joseph Silber

You can use the magic ::classconstant:

您可以使用魔术::class常量:

public function uniqueSlug($str, $model)
{
    $slug = Str::slug($str);

    $count = $model::whereRaw("slug RLIKE '^{$slug}(-[0-9]+)?$'")->count();

    return $count ? "{$slug}-{$count}" : $slug;
}

$newcat->slug = $helper->uniqueSlug($appname, Apk::class);

回答by kije

In PHP, classes (or class names) are handled as strings. Since PHP 5.5, you can use YourClass::classto get a fully qualified class name. If you want to get it in an earlier version of php, you can (if you have already an object of the calss) either do the following:

在 PHP 中,类(或类名)作为字符串处理。从 PHP 5.5 开始,您可以使用YourClass::class获取完全限定的类名。如果您想在较早版本的 php 中获取它,您可以(如果您已经拥有 calss 的对象)执行以下操作:

<?php
$obj = new YourClass();
// some code

$clazz = get_class($obj);
?>

or, you can implement a static method in your class, like this:

或者,您可以在类中实现静态方法,如下所示:

<?php

class YourClass {
    // some code

    public static function getClassName() {
        return get_called_class();
    }
?>

If you want to pass a class to a function, you can do it like this:

如果要将类传递给函数,可以这样做:

<?php
function do_somthing($arg1, $clazz) {
    $clazz::someStaticMethod($arg1);
}
?>

or

或者

<?php
function do_somthing($arg1, $clazz) {
    call_user_func(array($clazz, 'someStaticMethod')), $arg1);
}
?>

If you need to call a non-static method of that class, you need to instanciate it:

如果需要调用该类的非静态方法,则需要实例化它:

<?php
function do_somthing($arg1, $clazz) {
    $obj = new $clazz(); 
    $obj->someNonStaticMethod();
}
?>

Note:You can use PHP type hinting with passed class names:

注意:您可以对传递的类名使用 PHP 类型提示:

<?php
function do_somthing($arg1, MyInterface $clazz) {
    $obj = new $clazz(); 
    $obj->someInterfaceMethod();
}
?>

回答by CntkCtn

I think you can.

我想你可以。

Send the class name as string parameter then use it like below.

将类名作为字符串参数发送,然后像下面一样使用它。

$classtr = "yourparam";// param comes from the function call.

$obj = new $classtr;
$obj->method();

回答by Jose

Send the class name as string parameter you need use the namespace. For example:

将类名作为字符串参数发送,您需要使用命名空间。例如:

function defineClass()
{
  $class = "App\MyClass"; // mention the namespace too
}

function reciveClass($class)
{
   $class:: // what do you need,
}