Skip to content

Instantly share code, notes, and snippets.

@pniederlag
Last active August 29, 2015 14:14
Show Gist options
  • Save pniederlag/c61c2bd75a02c78ab71d to your computer and use it in GitHub Desktop.
Save pniederlag/c61c2bd75a02c78ab71d to your computer and use it in GitHub Desktop.
makeInstance adressing issue
Demonstrate the usage of makeInstance and/or php namespaces adressing background
<?php
namespace test {
class foo {
}
// fixed
$testFoo = new foo();
//$anotherFoo = test/foo(); // fatal
$testYetAnotherFoo = new \test\foo();
// dynamic
// $foo = 'foo'; // fatal
$fooFullyQualifiedName = '\\test\\foo';
$fooQualifiedName = 'test\\foo';
$testFooFullyQualified = new $fooFullyQualifiedName;
var_dump(get_class($testFooFullyQualified));
$testFooQualified = new $fooQualifiedName;
var_dump(get_class($testFooQualified));
}
namespace {
// fixed
// $testFoo = new foo(); // fatal
$anotherFoo = new test\foo();
$testYetAnotherFoo = new \test\foo();
echo "" . PHP_EOL;
// dynamic
$fooFullyQualifiedName = '\\test\\foo';
$fooQualifiedName = 'test\\foo';
$testFooFullyQualified = new $fooFullyQualifiedName;
var_dump(get_class($testFooFullyQualified));
$testFooQualified = new $fooQualifiedName;
var_dump(get_class($testFooQualified));
echo "" . PHP_EOL;
echo "Lessons learned:" . PHP_EOL;
echo "In *global* scope you can use 1) full qualified name *or* 2) qualified names" . PHP_EOL;
echo "php internally always represents the classname only *qualified*" . PHP_EOL;
echo "which is fine because from php perspective this is *global* namespace scope" . PHP_EOL;
echo "" . PHP_EOL;
}
/*
* in plain PHP
*/
// qualified relative (subnamespace) class instantiation
$fileUtility = new File\BasicFileUtility();
// fully qualified (absolute)
$fileUtility = new \TYPO3\CMS\Core\Utility\File\BasicFileUtility();
/*
* TYPO3 makeInstance or any other plain dynamic language context (variables as class names etc. pp')
*/
$fileUtility = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\File\\BasicFileUtility');
// Thx to @helhum for clearification/explanation
// we are using a dynamic language feature (classname as variable), thus it will be always interpreted in *global* namespace.
// So both fully qualified(leading slash) as well as qualified names do work from php perspective
// As internally the class name is stored qualified (without leading slash) it is easier to force to supply names qualified only.
@pniederlag
Copy link
Author

First of all many thx for your comment and very nice and exact explanations.

DEV without TYPO3 Background most likely would think this is "relative"

This has exactly NOTHING to do with TYPO3 background! Devs need to learn and understand that class names passed as strings are in global scope in PHP thus always fully qualified

full ACK,

So I blame PHP for

  • allowing both fully qalified(leading slash) as well as qualified names on global scope
  • representing class names qualified(sic!) internally, instead of fully qualified

Still to me the fully qualified name is more intuitive (while it might be redundant). But as now I got the whole picture of all the implications of fully qualified vs. qualified I got your point.

@pniederlag
Copy link
Author

btw, Updated the gist and put my essence "lessons learned" into demo.php.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment