小编Ant*_*ine的帖子

如何在PHP中的类属性中使用类常量?

这是不起作用的代码:

class MyClass
{
    const myconst = 'somevalue';

    private $myvar = array( 0 => 'do something with '.self::myconst );
}
Run Code Online (Sandbox Code Playgroud)

似乎类常量在"编译时"不可用,但仅在运行时.有谁知道任何解决方法?(定义不起作用)

谢谢

php oop const properties class

9
推荐指数
1
解决办法
3619
查看次数

在 PHP 中使用异常的正确方法是什么?

我一直使用“返回状态代码”方法来管理错误。看来异常现在是管理错误的最佳方式。
但它们真的是最好的方法吗?我读过类似异常比 GoTo 最糟糕的东西

您知道一些关于异常和错误管理的好文章/帖子/书籍吗?

php exception

5
推荐指数
1
解决办法
320
查看次数

如何在 Sonarque 中包含 Angular Material 样式?

我希望 Sonarqube 基于 Angular Material 分析我的 Angular 应用程序。
有这种样式:

.some-class {
    mat-icon {
        color: red;
    }
}
Run Code Online (Sandbox Code Playgroud)

由于 Angular Material 通过 angular.json 中的配置全局包含,Sonarqube 似乎无法看到 mat-icon 并喊出一个错误:

Unexpected unknown type selector "mat-icon"

有解决方法吗?一个配置?

sonarqube angular-material angular

5
推荐指数
1
解决办法
1002
查看次数

SmartPointer:在基类和派生类之间进行转换

假设你有这样的功能:

SmartPtr<A> doSomething(SmartPtr<A> a);
Run Code Online (Sandbox Code Playgroud)

像这样的类:

class A { }
class B : public A { }
Run Code Online (Sandbox Code Playgroud)

现在我这样做:

SmartPtr<A> foo = new B();
doSomething(foo);
Run Code Online (Sandbox Code Playgroud)

现在,我想从中取回一个SmartPtr<B>物体doSomething.

SmartPtr<B> b = doSomething(foo); 
Run Code Online (Sandbox Code Playgroud)

可能吗 ?我需要做什么样的铸造?
现在,我发现了一些我认为难看的东西:

B* b = (B*)doSomething().get()
Run Code Online (Sandbox Code Playgroud)

重要说明:我没有任何访问权限SmartPtrdoSomething()代码.

c++ inheritance casting smart-pointers

4
推荐指数
1
解决办法
6410
查看次数

对象拒绝自己是否正确?

我想解析几种文件格式.我想知道是否正确的OOP"冒风险"创造一个无用的对象.

class ParserFactory
{
    private fn;

    public function ParserFactory(fn)
    {
        this->fn = fn;
    }
    public function getParser()
    {
        a = new FormatAParser(this->fn);
        if ( a->isValid() )
        {
            return( a );
        }
        b = new FormatBParser(this->fn);
        // ... and so on...
    }
}

class FormatAParser
{
    /*
        The object is telling us if is able to continue to work...
        **CLEAN OR DIRTY DESIGN ?**
    */
    public function isValid()
    {            
        header = SomeConversionAndReadingStuff();
        if ( header != "formatA" )
        {
            return(false)
        } …
Run Code Online (Sandbox Code Playgroud)

oop format file detection

2
推荐指数
1
解决办法
66
查看次数