国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

Laravel學(xué)習(xí)筆記之PHP反射(Reflection) (上)

JessYanCoding / 1462人閱讀

摘要:說明中經(jīng)常使用的反射特性來設(shè)計(jì)代碼,本文主要學(xué)習(xí)的反射特性,來提高寫代碼時(shí)的設(shè)計(jì)質(zhì)量。提供一套檢測的兩個(gè)工具包和,類似于探針一樣的東西來探測這些一等公民。限于篇幅,下篇再聊下反射。

說明:Laravel中經(jīng)常使用PHP的反射特性來設(shè)計(jì)代碼,本文主要學(xué)習(xí)PHP的反射特性,來提高寫代碼時(shí)的設(shè)計(jì)質(zhì)量。PHP提供一套檢測class, interface, trait, property, method的兩個(gè)工具包:Introspection FunctionsReflection API,類似于探針一樣的東西來探測這些一等公民。本文先看下Introspection Functions的使用。

開發(fā)環(huán)境: Laravel5.3 + PHP7

Introspection Functions

Introspection Functions是用來操作object class的一些函數(shù),PHP提供了大量的Introspection Functions來操作class, interface, trait, method, property:

class_exists()

interface_exists()

method_exists()

property_exists()

trait_exists()

class_alias()

get_class()

get_parent_class()

get_called_class()

get_class_methods()

get_class_vars()

get_object_vars()

is_subclass_of()

is_a()

class_exists()

Laravel源碼中好多個(gè)地方使用到class_exists()方法來判斷指定類是否存在,如IlluminateDatabaseConnection::isDoctrineAvailable()的源碼:

    public function isDoctrineAvailable()
    {
        return class_exists("DoctrineDBALConnection"); // DoctrineDBALConnection::class類是否存在,大小寫不敏感
    }

寫個(gè)PHPUnit測試下(爆綠燈,說明是正確的,這里不截圖了。后面所有Introspection的測試都放在IntrospectionTest這個(gè)單元測試?yán)?:

namespace MyRightCapitalContainerTests;

class IntrospectionTest extends PHPUnit_Framework_TestCase
{
    public function testClassExists()
    {
        // Arrange
        
        // Actual
        $class_exists = class_exists(TestClassExists::class);
        // Assert
        $this->assertTrue($class_exists);
    }
}

class TestClassExists
{
    
}
interface_exists()

interface_exists()是用來檢查接口是否存在,寫個(gè)PHPUnit測試下,爆綠燈:

namespace MyRightCapitalContainerTests;

class IntrospectionTest extends PHPUnit_Framework_TestCase
{
    public function testInterfaceExists()
    {
        // Arrange
        
        // Actual
        $interface_exists = interface_exists(TestInterfaceExists::class);
        // Assert
        $this->assertTrue($interface_exists);
    }
}

interface TestInterfaceExists
{
    
}
method_exists()

檢查類的方法(private,protected,public)是否存在于指定的類對(duì)象或類名中,Laravel中很多處用到了這個(gè)函數(shù),如Application中的register()檢查service provider中register是否存在,和bootProvider()中檢查service provider中boot()方法是否存在:

public function register($provider, $options = [], $force = false)
{
    ...
    
    if (method_exists($provider, "register")) {
            $provider->register();
    }
    
    ...
}
protected function bootProvider(ServiceProvider $provider)
{
    if (method_exists($provider, "boot")) {
        return $this->call([$provider, "boot"]);
    }
}

這里寫個(gè)PHPUnit測試下,爆綠燈:

    public function testMethodExists()
    {
        // Arrange
        $test_class_exists = new TestClassExists();
        
        // Actual
        $object_method_exists1    = method_exists($test_class_exists, "testPrivateMethodExists");
        $object_method_exists2    = method_exists($test_class_exists, "testProtectedMethodExists");
        $object_method_exists3    = method_exists($test_class_exists, "testPublicMethodExists");
        $classname_method_exists1 = method_exists(TestClassExists::class, "testPrivateMethodExists");
        $classname_method_exists2 = method_exists(TestClassExists::class, "testProtectedMethodExists");
        $classname_method_exists3 = method_exists(TestClassExists::class, "testPublicMethodExists");
        
        // Assert
        $this->assertTrue($object_method_exists1);
        $this->assertTrue($object_method_exists2);
        $this->assertTrue($object_method_exists3);
        $this->assertTrue($classname_method_exists1);
        $this->assertTrue($classname_method_exists2);
        $this->assertTrue($classname_method_exists3);
    }
    
    
    class TestClassExists
    {
        private function testPrivateMethodExists()
        {
        }
        
        protected function testProtectedMethodExists()
        {
        }
        
        public function testPublicMethodExists()
        {
        }
    }
property_exists()

檢查該屬性(private, protected, public)是否存在于類對(duì)象或類名中,Laravel很多地方用到了該函數(shù),如IlluminateFoundationAuthRedirectsUsers::redirectPath()源碼:

    public function redirectPath()
    {
        return property_exists($this, "redirectTo") ? $this->redirectTo : "/home";
    }

寫個(gè)PHPUnit測試下該函數(shù),爆綠燈:

    // class IntrospectionTest
    public function testPropertyExists()
    {
        // Arrange
        $test_class_exists = new TestClassExists();
        
        // Actual
        $private_property1   = property_exists($test_class_exists, "testPrivatePropertyExists");
        $private_property2   = property_exists(TestClassExists::class, "testPrivatePropertyExists");
        $protected_property1 = property_exists($test_class_exists, "testProtectedPropertyExists");
        $protected_property2 = property_exists(TestClassExists::class, "testProtectedPropertyExists");
        $public_property1    = property_exists($test_class_exists, "testPublicPropertyExists");
        $public_property2    = property_exists(TestClassExists::class, "testPublicPropertyExists");
        
        // Assert
        $this->assertTrue($private_property1);
        $this->assertTrue($private_property2);
        $this->assertTrue($protected_property1);
        $this->assertTrue($protected_property2);
        $this->assertTrue($public_property1);
        $this->assertTrue($public_property2);
    }
    
    
    class TestClassExists
    {
        private   $testPrivatePropertyExists;
        protected $testProtectedPropertyExists;
        public    $testPublicPropertyExists;
    }
trait_exists()

檢查trait是否存在,寫下PHPUnit測試,爆綠燈:

    // class IntrospectionTest
    public function testTraitExists()
    {
        // Arrange
        
        // Actual
        $test_trait_exists = trait_exists(TestTraitExists::class);
        
        // Assert
        $this->assertTrue($test_trait_exists);
    }
    
    trait TestTraitExists
    {
    
    }
class_alias()

給指定類取別名,Laravel中只有一處使用了class_alias(),用來給config/app.php中$aliases[ ]注冊(cè)別名,可看下Laravel學(xué)習(xí)筆記之bootstrap源碼解析,看下Laravel中如何使用的:

    public function load($alias)
    {
        if (isset($this->aliases[$alias])) {
            return class_alias($this->aliases[$alias], $alias);
        }
    }

寫個(gè)PHPUnit測試,爆綠燈:

    public function testClassAlias()
    {
        // Arrange
        class_alias(TestClassExists::class, "MyRightCapitalContainerTestsAliasTestClassExists");
        $test_class_exists = new TestClassExists();
        
        // Actual
        $actual = new AliasTestClassExists();
        
        //Assert
        $this->assertInstanceOf(TestClassExists::class, $actual);
        $this->assertInstanceOf(AliasTestClassExists::class, $test_class_exists);
    }
get_class()

get_class()獲取對(duì)象的類名,這個(gè)函數(shù)在Laravel中大量地方在用了,如Application::getProvider($provider)方法,是個(gè)很好用的方法:

    public function getProvider($provider)
    {
        $name = is_string($provider) ? $provider : get_class($provider);

        return Arr::first($this->serviceProviders, function ($value) use ($name) {
            return $value instanceof $name;
        });
    }

寫個(gè)PHPUnit測試,爆綠燈:

    public function testGetClass()
    {
        // Arrange
        $test_class_exists = new TestClassExists();
        
        // Actual
        $class_name = get_class($test_class_exists);
        
        // Assert
        $this->assertSame(TestClassExists::class, $class_name);
    }
get_parent_class()

get_parent_class()是用來獲取類的父類名,目前Laravel中還沒用到這個(gè)函數(shù),傳入的可以是子類對(duì)象或者子類名,寫個(gè)PHPUnit測試下:

    // namespace MyRightCapitalContainerTests;
    // class IntrospectionTest extends PHPUnit_Framework_TestCase
    public function testGetParentClass()
    {
        // Arrange
        $child_class = new ChildClass();
        
        // Actual
        $parent_class1 = get_parent_class($child_class);
        $parent_class2 = get_parent_class(ChildClass::class);
        
        // Assert
        $this->assertSame(ParentClass::class, $parent_class1);
        $this->assertSame(ParentClass::class, $parent_class2);
    }
    
    class ChildClass extends ParentClass 
    {
        
    }
    
    class ParentClass
    {
        
    }
get_called_class()

get_called_class()獲取后期靜態(tài)綁定類即實(shí)際調(diào)用類的名稱,Laravel中還沒使用到該函數(shù),不妨寫個(gè)測試看下如何使用:

    // namespace MyRightCapitalContainerTests;
    // class IntrospectionTest extends PHPUnit_Framework_TestCase
    public function testGetCalledClass()
    {
        // Arrange
        $child_class  = new ChildClass();
        $parent_class = new ParentClass();
        
        // Actual
        $child_called_class = $child_class->testGetCalledClass();
        $parent_called_class = $parent_class->testGetCalledClass();
        
        // Assert
        $this->assertSame(ChildClass::class, $child_called_class);
        $this->assertSame(ParentClass::class, $parent_called_class);
    }
    
    class ChildClass extends ParentClass
    {
        
    }
    
    class ParentClass
    {
        public function testGetCalledClass()
        {
            return get_called_class();
        }
    }
get_class_methods()

get_class_methods()用來獲取類的方法名組成一個(gè)數(shù)組(測試只能是public),Laravel只有一處用到了該方法IlluminateDatabaseEloquentModel::cacheMutatedAttributes() :line 3397,這里寫個(gè)PHPUnit測試,爆綠燈:

    public function testGetClassMethod()
    {
        // Arrange
        $get_class_methods1 = get_class_methods(ChildClass::class);
        $get_class_methods2 = get_class_methods(new ChildClass());
        
        // Actual
        
        // Assert
        $this->assertFalse(in_array("testPrivateGetClassMethod", $get_class_methods1, true));
        $this->assertFalse(in_array("testPrivateGetClassMethod", $get_class_methods2, true));
        $this->assertFalse(in_array("testProtectedGetClassMethod", $get_class_methods1, true));
        $this->assertFalse(in_array("testProtectedGetClassMethod", $get_class_methods2, true));
        $this->assertTrue(in_array("testPublicGetClassMethod", $get_class_methods1, true));
        $this->assertTrue(in_array("testPublicGetClassMethod", $get_class_methods2, true));
        $this->assertTrue(in_array("testGetCalledClass", $get_class_methods1, true));
        $this->assertTrue(in_array("testGetCalledClass", $get_class_methods2, true));
    }
    
    class ChildClass extends ParentClass
    {
        private function testPrivateGetClassMethod()
        {
        }
        
        protected function testProtectedGetClassMethod()
        {
        }
        
        public function testPublicGetClassMethod()
        {
        }
    }
get_class_vars()

get_class_vars()只會(huì)讀取類的public屬性組成一個(gè)數(shù)組,類似于get_class_methods(),若屬性沒有默認(rèn)值就為null,目前Laravel中還未使用,看下PHPUnit測試:

    public function testGetClassVars()
    {
        // Arrange
        
        // Actual
        $class_vars = get_class_vars(ChildClass::class);
        
        // Assert
        $this->assertArrayNotHasKey("privateNoDefaultVar", $class_vars);
        $this->assertArrayNotHasKey("privateDefaultVar", $class_vars);
        $this->assertArrayNotHasKey("protectedNoDefaultVar", $class_vars);
        $this->assertArrayNotHasKey("protectedDefaultVar", $class_vars);
        $this->assertEmpty($class_vars["publicNoDefaultVar"]);
        $this->assertEquals("public_laravel", $class_vars["publicDefaultVar"]);
    }
    
    class ChildClass extends ParentClass
    {
        private   $privateNoDefaultVar;
        private   $privateDefaultVar   = "private_laravel";
        protected $protectedNoDefaultVar;
        protected $protectedDefaultVar = "protected_laravel";
        public    $publicNoDefaultVar;
        public    $publicDefaultVar    = "public_laravel";
   }     
get_object_vars()

get_object_vars()只會(huì)讀取對(duì)象的public屬性組成一個(gè)數(shù)組,類似于get_class_vars(), get_class_methods(),且屬性沒有默認(rèn)值就是null,Laravel中只有一處使用到IlluminateMailJobsHandleQueuedMessage::__sleep() :line 78,寫個(gè)PHPUnit測試下,爆綠燈:

    public function testGetObjectVars()
    {
        // Arrange
        $get_object_vars = new TestGetObjectVars(1, 2, 3);
        
        // Actual
        $object_vars = get_object_vars($get_object_vars);
        
        // Assert
        $this->assertArrayNotHasKey("x", $object_vars);
        $this->assertArrayNotHasKey("y", $object_vars);
        $this->assertEquals(3, $object_vars["z"]);
        $this->assertArrayNotHasKey("dot1", $object_vars);
        $this->assertArrayNotHasKey("dot2", $object_vars);
        $this->assertArrayNotHasKey("circle1", $object_vars);
        $this->assertArrayNotHasKey("circle2", $object_vars);
        $this->assertEquals(10, $object_vars["line1"]);
        $this->assertEmpty($object_vars["line2"]);
    }
    
    class TestGetObjectVars
    {
        private   $x;
        protected $y;
        public    $z;
        private   $dot1    = 10;
        private   $dot2;
        protected $circle1 = 20;
        protected $circle2;
        public    $line1   = 10;
        public    $line2;
        
        public function __construct($x, $y, $z)
        {
            
            $this->x = $x;
            $this->y = $y;
            $this->z = $z;
        }
    }
is_subclass_of()

is_subclass_of()用來判斷給定類對(duì)象是否是另一給定類名的子類,Laravel中有用到,這里寫下PHPUnit測試,爆綠燈:

    public function testIsSubclassOf()
    {
        // Arrange
        $child_class = new ChildClass();
        
        // Actual
        $is_subclass = is_subclass_of($child_class, ParentClass::class);
        
        // Assert
        $this->assertTrue($is_subclass);
    }
is_a()

is_a()用來判定給定類對(duì)象是否是另一給定類名的對(duì)象或是子類,和is_subclass_of()有點(diǎn)類似,只是is_a()還可以判定是不是該類的對(duì)象,is_a()類似于instanceof操作符,Laravel中還沒用到這個(gè)方法,這里寫個(gè)PHPUnit測試,爆綠燈:

    public function testIsA()
    {
        // Arrange
        $child_class = new ChildClass();
        
        // Actual
        $is_object   = is_a($child_class, ChildClass::class);
        $is_subclass = is_a($child_class, ParentClass::class);
        
        // Assert
        $this->assertTrue($is_object);
        $this->assertTrue($is_subclass);
    }

最后,給下整個(gè)PHPUnit的測試代碼:

assertTrue($class_exists);
    }
    
    public function testInterfaceExists()
    {
        // Arrange
        
        // Actual
        $interface_exists = interface_exists(TestInterfaceExists::class);
        // Assert
        $this->assertTrue($interface_exists);
    }
    
    public function testMethodExists()
    {
        // Arrange
        $test_class_exists = new TestClassExists();
        
        // Actual
        $object_method_exists1    = method_exists($test_class_exists, "testPrivateMethodExists");
        $object_method_exists2    = method_exists($test_class_exists, "testProtectedMethodExists");
        $object_method_exists3    = method_exists($test_class_exists, "testPublicMethodExists");
        $classname_method_exists1 = method_exists(TestClassExists::class, "testPrivateMethodExists");
        $classname_method_exists2 = method_exists(TestClassExists::class, "testProtectedMethodExists");
        $classname_method_exists3 = method_exists(TestClassExists::class, "testPublicMethodExists");
        
        // Assert
        $this->assertTrue($object_method_exists1);
        $this->assertTrue($object_method_exists2);
        $this->assertTrue($object_method_exists3);
        $this->assertTrue($classname_method_exists1);
        $this->assertTrue($classname_method_exists2);
        $this->assertTrue($classname_method_exists3);
    }
    
    public function testPropertyExists()
    {
        // Arrange
        $test_class_exists = new TestClassExists();
        
        // Actual
        $private_property1   = property_exists($test_class_exists, "testPrivatePropertyExists");
        $private_property2   = property_exists(TestClassExists::class, "testPrivatePropertyExists");
        $protected_property1 = property_exists($test_class_exists, "testProtectedPropertyExists");
        $protected_property2 = property_exists(TestClassExists::class, "testProtectedPropertyExists");
        $public_property1    = property_exists($test_class_exists, "testPublicPropertyExists");
        $public_property2    = property_exists(TestClassExists::class, "testPublicPropertyExists");
        
        // Assert
        $this->assertTrue($private_property1);
        $this->assertTrue($private_property2);
        $this->assertTrue($protected_property1);
        $this->assertTrue($protected_property2);
        $this->assertTrue($public_property1);
        $this->assertTrue($public_property2);
    }
    
    public function testTraitExists()
    {
        // Arrange
        
        // Actual
        $test_trait_exists = trait_exists(TestTraitExists::class);
        
        // Assert
        $this->assertTrue($test_trait_exists);
    }
    
    public function testClassAlias()
    {
        // Arrange
        class_alias(TestClassExists::class, "MyRightCapitalContainerTestsAliasTestClassExists");
        $test_class_exists = new TestClassExists();
        
        // Actual
        $actual = new AliasTestClassExists();
        
        //Assert
        $this->assertInstanceOf(TestClassExists::class, $actual);
        $this->assertInstanceOf(AliasTestClassExists::class, $test_class_exists);
    }
    
    public function testGetClass()
    {
        // Arrange
        $test_class_exists = new TestClassExists();
        
        // Actual
        $class_name = get_class($test_class_exists);
        
        // Assert
        $this->assertSame(TestClassExists::class, $class_name);
    }
    
    public function testGetParentClass()
    {
        // Arrange
        $child_class = new ChildClass();
        
        // Actual
        $parent_class1 = get_parent_class($child_class);
        $parent_class2 = get_parent_class(ChildClass::class);
        
        // Assert
        $this->assertSame(ParentClass::class, $parent_class1);
        $this->assertSame(ParentClass::class, $parent_class2);
    }
    
    public function testGetCalledClass()
    {
        // Arrange
        $child_class  = new ChildClass();
        $parent_class = new ParentClass();
        
        // Actual
        $child_called_class  = $child_class->testGetCalledClass();
        $parent_called_class = $parent_class->testGetCalledClass();
        
        // Assert
        $this->assertSame(ChildClass::class, $child_called_class);
        $this->assertSame(ParentClass::class, $parent_called_class);
    }
    
    public function testInArray()
    {
        $this->assertTrue(in_array("a", ["a", "b", 1], true));
    }
    
    public function testGetClassMethod()
    {
        // Arrange
        $get_class_methods1 = get_class_methods(ChildClass::class);
        $get_class_methods2 = get_class_methods(new ChildClass());
        
        // Actual
        
        // Assert
        $this->assertFalse(in_array("testPrivateGetClassMethod", $get_class_methods1, true));
        $this->assertFalse(in_array("testPrivateGetClassMethod", $get_class_methods2, true));
        $this->assertFalse(in_array("testProtectedGetClassMethod", $get_class_methods1, true));
        $this->assertFalse(in_array("testProtectedGetClassMethod", $get_class_methods2, true));
        $this->assertTrue(in_array("testPublicGetClassMethod", $get_class_methods1, true));
        $this->assertTrue(in_array("testPublicGetClassMethod", $get_class_methods2, true));
        $this->assertTrue(in_array("testGetCalledClass", $get_class_methods1, true));
        $this->assertTrue(in_array("testGetCalledClass", $get_class_methods2, true));
    }
    
    public function testGetClassVars()
    {
        // Arrange
        
        // Actual
        $class_vars = get_class_vars(ChildClass::class);
        
        // Assert
        $this->assertArrayNotHasKey("privateNoDefaultVar", $class_vars);
        $this->assertArrayNotHasKey("privateDefaultVar", $class_vars);
        $this->assertArrayNotHasKey("protectedNoDefaultVar", $class_vars);
        $this->assertArrayNotHasKey("protectedDefaultVar", $class_vars);
        $this->assertEmpty($class_vars["publicNoDefaultVar"]);
        $this->assertEquals("public_laravel", $class_vars["publicDefaultVar"]);
    }
    
    public function testGetObjectVars()
    {
        // Arrange
        $get_object_vars = new TestGetObjectVars(1, 2, 3);
        
        // Actual
        $object_vars = get_object_vars($get_object_vars);
        
        // Assert
        $this->assertArrayNotHasKey("x", $object_vars);
        $this->assertArrayNotHasKey("y", $object_vars);
        $this->assertEquals(3, $object_vars["z"]);
        $this->assertArrayNotHasKey("dot1", $object_vars);
        $this->assertArrayNotHasKey("dot2", $object_vars);
        $this->assertArrayNotHasKey("circle1", $object_vars);
        $this->assertArrayNotHasKey("circle2", $object_vars);
        $this->assertEquals(10, $object_vars["line1"]);
        $this->assertEmpty($object_vars["line2"]);
    }
    
    public function testIsSubclassOf()
    {
        // Arrange
        $child_class = new ChildClass();
        
        // Actual
        $is_subclass = is_subclass_of($child_class, ParentClass::class);
        
        // Assert
        $this->assertTrue($is_subclass);
    }
    
    public function testIsA()
    {
        // Arrange
        $child_class = new ChildClass();
        
        // Actual
        $is_object = is_a($child_class, ChildClass::class);
        $is_subclass = is_a($child_class, ParentClass::class);
        
        // Assert
        $this->assertTrue($is_object);
        $this->assertTrue($is_subclass);
    }
}

class TestGetObjectVars
{
    private   $x;
    protected $y;
    public    $z;
    private   $dot1    = 10;
    private   $dot2;
    protected $circle1 = 20;
    protected $circle2;
    public    $line1   = 10;
    public    $line2;
    
    public function __construct($x, $y, $z)
    {
        
        $this->x = $x;
        $this->y = $y;
        $this->z = $z;
    }
}

class ChildClass extends ParentClass
{
    private   $privateNoDefaultVar;
    private   $privateDefaultVar   = "private_laravel";
    protected $protectedNoDefaultVar;
    protected $protectedDefaultVar = "protected_laravel";
    public    $publicNoDefaultVar;
    public    $publicDefaultVar    = "public_laravel";
    
    private function testPrivateGetClassMethod()
    {
    }
    
    protected function testProtectedGetClassMethod()
    {
    }
    
    public function testPublicGetClassMethod()
    {
    }
}

class ParentClass
{
    public function testGetCalledClass()
    {
        return get_called_class();
    }
}

class TestClassExists
{
    private   $testPrivatePropertyExists;
    protected $testProtectedPropertyExists;
    public    $testPublicPropertyExists;
    
    private function testPrivateMethodExists()
    {
    }
    
    protected function testProtectedMethodExists()
    {
    }
    
    public function testPublicMethodExists()
    {
    }
}

interface TestInterfaceExists
{
    
}

trait TestTraitExists
{
    
}

PHP不僅提供了檢測class, interface, trait, property, method這些函數(shù)Introspection Functions,還提供了一整套的API即反射來檢測class, interface, trait, property, method,這些API是好幾個(gè)類組成的,提供了很多好用的方法。限于篇幅,下篇再聊下反射API。

總結(jié):本文主要聊了下PHP提供的一套檢測class, interface, trait, property, method的兩個(gè)工具包:Introspection Functions和Reflection API,這里先聊到Introspection Functions。下篇再聊下Reflection API的使用,到時(shí)見。

歡迎關(guān)注Laravel-China。

RightCapital招聘Laravel DevOps

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/30477.html

相關(guān)文章

  • Laravel深入學(xué)習(xí)2 - 控制反轉(zhuǎn)容器

    摘要:控制反轉(zhuǎn)容器控制反轉(zhuǎn)使依賴注入變得更加便捷。有瑕疵控制反轉(zhuǎn)容器是實(shí)現(xiàn)的控制翻轉(zhuǎn)容器的一種替代方案。容器的獨(dú)立使用即使沒有使用框架,我們?nèi)匀豢梢栽陧?xiàng)目中使用安裝組件來使用的控制反轉(zhuǎn)容器。在沒有給定任何信息的情況下,容器是無法實(shí)例化相關(guān)依賴的。 聲明:本文并非博主原創(chuàng),而是來自對(duì)《Laravel 4 From Apprentice to Artisan》閱讀的翻譯和理解,當(dāng)然也不是原汁原味...

    worldligang 評(píng)論0 收藏0
  • PHP高級(jí)特性-反射Reflection以及Factory工廠設(shè)計(jì)模式的結(jié)合使用[代碼實(shí)例]

    摘要:反射提供給面向?qū)ο缶幊炭梢宰允〉哪芰Γ捶瓷洹T诤唵喂S模式中,根據(jù)傳遞的參數(shù)來返回不同的類的實(shí)例簡單工廠模式又稱為靜態(tài)工廠方法模式。也就是簡單工廠模式工廠工廠類。PHP高級(jí)特性-反射以及工廠設(shè)計(jì)模式的結(jié)合使用 [結(jié)合 Laravel-Admin 代碼實(shí)例講解]利用反射來實(shí)現(xiàn)工廠模式的生產(chǎn)而無需創(chuàng)建特定的工廠類本文地址http://janrs.com/?p=833轉(zhuǎn)載無需經(jīng)過作者本人授權(quán)轉(zhuǎn)載...

    番茄西紅柿 評(píng)論0 收藏2637
  • PHP反射機(jī)制

    摘要:反射機(jī)制反射機(jī)制從開始支持,做業(yè)務(wù)開發(fā)的話應(yīng)該很少接觸反射。我的理解就是反射機(jī)制能拿到類里面的屬性方法,和的也可以以上是官方文檔中給出的東西,說實(shí)話我看了感覺沒什么感覺。在容器成員變量中數(shù)組維護(hù)這個(gè)類,反射實(shí)例調(diào)用構(gòu)造函數(shù),獲取返回值。 PHP反射機(jī)制 PHP反射機(jī)制從PHP5開始支持,做業(yè)務(wù)開發(fā)的話應(yīng)該很少接觸反射。我其實(shí)也是接觸不多,最近在學(xué)習(xí)laravel的優(yōu)雅,就接觸了到它其中...

    URLOS 評(píng)論0 收藏0
  • Essential SQLAlchemy2th學(xué)習(xí)筆記反射Reflection

    摘要:基于反射對(duì)象進(jìn)行查詢模塊反射這里我們不再使用而是使用擴(kuò)展模塊的獲取所有的對(duì)象名獲取表對(duì)象進(jìn)行操作反射關(guān)聯(lián)關(guān)系可以反射并建立表之間的但是建立關(guān)聯(lián)列的命名為例如關(guān)于更多信息請(qǐng)?jiān)敿?xì)參看官方文檔 示例數(shù)據(jù)庫下載:http://chinookdatabase.codepl...在SQLALchemy中,我們使用反射技術(shù)來獲取相關(guān)database schema信息,如tables,views,in...

    NSFish 評(píng)論0 收藏0
  • 詳解 Laravel 中的依賴注入和 IoC

    摘要:依賴注入依賴注入一詞是由提出的術(shù)語,它是將組件注入到應(yīng)用程序中的一種行為。就像說的依賴注入是敏捷架構(gòu)中關(guān)鍵元素。類依賴于,所以我們的代碼可能是這樣的創(chuàng)建一個(gè)這是一種經(jīng)典的方法,讓我們從使用構(gòu)造函數(shù)注入開始。 showImg(https://segmentfault.com/img/remote/1460000018806800); 文章轉(zhuǎn)自:https://learnku.com/la...

    haitiancoder 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<