PHP code example of macfja / composer-class-rewrite
1. Go to this page and download the library: Download macfja/composer-class-rewrite library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
macfja / composer-class-rewrite example snippets
# File: example/A.php
namespace Example;
class A
{
public function who()
{
return 'A';
}
}
# File: example/B.php
namespace Example;
class B extends A
{
public function who()
{
return parent::who() . '-B';
}
}
# File: example/C.php
namespace Example;
class C extends A implements \MacFJA\ClassRewrite\Rewriter
{
public function who()
{
return parent::who() . '-C';
}
}
$b = new B();
echo $b->who(); // Output: "A-C-B"
# File: example/A.php
namespace Example;
class A
{
public function who()
{
return 'A';
}
}
# File: example/B.php
namespace Example;
class B extends A implements \MacFJA\ClassRewrite\Rewriter
{
public function who()
{
return parent::who() . '-B';
}
}
# File: example/C.php
namespace Example;
class C extends B
{
public function who()
{
return parent::who() . '-C';
}
}
# File: test.php
;
echo $class->who(); //output A-B
$class2 = new B();
echo $class2->who(); // output A-B-B
// -> the output is (A='A-B', parent of B) + '-B' (from B)
$class3 = new C();
echo $class3->who(); // output A-B-B-C
// -> the output is (A='A-B', parent of B) + '-B' (from B, parent of C) + '-C' (from C)
# File: example/A.php
namespace Example;
class A
{
public function dir()
{
return __DIR__; // "~/example"
}
public function thisClass()
{
return __CLASS__; // "Example\A"
}
}
# File: example/B.php
namespace Example;
class B extends A implements \MacFJA\ClassRewrite\Rewriter
{
public function dir()
{
return __DIR__; // "~/example"
}
}