我想在同一个名称空间中的两个不同文件中Use位于App\Helpers\Helper的同一个类Helper。
例如:
类A:
<?php
namespace App\Services;
Use Helper;
class A {...}
B类:
<?php
namespace App\Services;
Use Helper;
class B {...}
但是,这是不允许的。错误是:
Cannot use App\Helpers\Helper as Helper because the name is already in use
我可以在第二个文件中将Helper类重
我使用了另一个类作为依赖注入,它是好的工作,或者我已经搞乱了OOP的方式。
Helper.php
class Helper {
public function getModulePermission($role_id, $module, $type) {
// my work code
}
}
DesignationController.php
use App\Helpers\Helper;
class DesignationController extends Controller {
protected $designation;
p
编辑:我提交了一个。
有办法将私有字段从一个类移动到它的助手类吗?下面的小鸡抓取UML显示了我现在正在手动做的事情。类C1在重构之前具有私有field和对Helper对象的私有最终引用。
在重构之后,C1'中对field的所有引用都将酌情更改为helper.getField()和helper.setfield()。
class Field {}
class C1 {
final private Field field;
final private Helper helper;
public Field getField() {
return
我希望创建一个包含cleanArray、split_char、split_word等方法的助手类。
helper类--它是self --将与许多类一起使用。例子:
Class A will user Helper, Class B, Class C, D, E also user Helper Class
在PHP中编写和使用helper类的最佳方法是什么?
我所知道的是OOP的基本知识,即在每个使用Helper类的类中都必须创建一个helper对象。
$helper = new Helper();
这是正确的,也可能是某个人,可以给我最好的方法去做。
我还将创建XXX类,可以使用A类、B类、
我有一个名为Zend_View_Helper_FormVars的助手,它由我的一个模块使用。我在application/common/helpers/GeneralFunctions.php中也有一个常见的助手
我试图从Zend_View_Helper_FormVars调用GeneralFunctions.php中的一个函数。
以下是Zend_View_Helper_FormVars的简短版本
class Zend_View_Helper_FormVars
{
public $reqFieldVisual='<span class="req">*&l
我有一个类以类似的方式使用一组属性(为简洁起见,在示例中只显示了两个属性)。一般行为是在基类上定义的,而特定行为是在特定接口中定义的。
问题是:如果我将它们声明为基类,则必须将它们转换为接口以调用接口方法。现在,如果我将它们声明为接口,则当我要调用基方法时,必须将它们转换为基类。
在这里使用接口时,我的目标是提高可测试性(稍后使用依赖项注入),并养成“对接口进行编程”的习惯,但我不能决定哪一种方式最好,或者即使整个理论基础一开始是好的。
public class Conductor
{
// These properties inherit from base class
//
有许多文章描述了如何在PHP-5.3+中使用名称空间,我对use语句中的约定特别感兴趣。
大多数文章没有指出的是,use可以引用名称空间或类。因此,要在PSR-0位置Foobar/Helper/Helper.php中引用我的类,我可以这样做:
use \Foobar\Helper; // This is a namespace
class Whatever {
...
Helper\Helper::MyHelperMethod();
或者这个:
use \Foobar\Helper\Helper; // This is a class
class Whatever {
因此,我正在尝试使用helper方法构造一个类,即:
class Type{
int a, b, c;
friend auto helper(auto);
friend auto test_helper(auto);
/* couples test with implement */
public:
void method(){
helper(this);
}
};
但是,如果我们想要测试helper,那么让helper成为好友函数可以将测试与实现结合起来。
所以我想让helper成为一个免费的函数,即:
auto helpe
在为C++中的类编写帮助器方法时,是否应该在头文件(.h)中的类定义中将其声明为私有方法?例如:
/*Foo.h*/
class Foo {
public:
int bar();
private:
int helper();
};
...
/*Foo.cpp*/
...
int foo::bar() {
int something = this->helper();
}
int foo::helper() {
...
}
或者,最好不要将其声明为类的私有成员,而只是将其作为实现中的独立函数?
/*Foo.h*/
我有一个家庭作业,必须完成类似的事情:
module Foo
def self.bar
yield
end
def helper (number)
p number
end
end
Foo.bar do
helper 5
end
这当然会给出一个错误,因为'helper‘没有在Object中定义。但在任务中,它直截了当地说,Foo必须以这种方式使用:
Foo.bar do
helper 5
end
它没有说明“helper”是在哪里定义的。我怎么能像上面这样调用这个方法呢?
我已经使用了部分类修饰符一段时间,以便将助手类放入它们自己的文件中。
今天,我们有了一个新的家伙,他说他的最后一个团队不允许这个部分类,因为修改一个位于单独文件中的helper类会导致主分部类文件因更改而变得不正常。另外,他们只允许在主类中放置一个助手类,作为最后的手段,这样所有的东西都保持解耦。
你认为如何?使用这样的部分类有什么问题吗?还是归结为首选类?
例如,我通常有这样的事情:
MainClass.cs
MainClass.Helper1.cs
MainClass.Helper2.cs
..。
// Inside of MainClass.cs I have cod
我举个例子:
requirement:按照以下规则构造一条材料:
在给定的< 100 =>材料中是绿色的
<300=>材料中100 <黄色
其中> 300=>材料是红色的
我的考试课:
class MaterialTest{
MaterialConstructor materialCons
public void testBuild(){
materialCons.setAmount(150);
Material material
对于小型助手实用程序类的集合,我创建了一个通用的class MyUtils
// MyUtils.java
public final class MyUtils
{
public static class Helper1 {};
public static class Helper2 {};
//...
}
这个来自MyUtils内部的帮助类将在包的其他文件中使用:
// MyClass1.java
public class MyClass1
{
private MyUtils.Helper1 help1 = new MyUtils.Helper1();
public void
它对require_once是正确的吗?你会把它放在哪里,如何放入include path?
它不应该在application.ini或引导程序中吗?
示例:
require_once 'Zend/View/Helper/Abstract.php';
// @question - is this correct - where and
// how would you put it include path
class Zend_View_Helper_Translate extends Zend_View_Helper_Abstract
{
}
从线程的角度来看,静态引用和易失性引用有什么不同?例如,我想要一个单例对象,我的代码如下:
class Singleton {
Helper helper; /*Shall I make this variable static or volatile*/
Helper getHelper() {
if(helper==null) {
synchronized(this) {
if(helper==null) {
helper=new Hel
这个问题的出现是为了区分util类和helper类。
我认为helper类可以有复杂的字段,而util类应该只包含静态方法。你认为下面的例子作为一个帮助器类合适吗?或者Helper类不能包含任何依赖项?例如:
class BusinessDataHelper(){
private BusinessDataService service;
public Result getData(int id){
service.getData(id)
}
}
我对helper类的理解是通过在helper类中添加可以被多个类使用的方法来减少重复代码。并在