PHP - Accessing Parent's Namespace
Is it possible to have a class access another class it its parent directory without using a fully qualified name? While the following script is not valid, it is how I would have thought it would be done should it be possible. If it is not possible, I expect there is a good reason why one would not want to do so, and would appreciate your thoughts why that is so. Thanks // src/PrimaryTopic/SubTopic/SubSubTopic/ParentClass.php namespace PrimaryTopic\SubTopic\SubSubTopic; class ParentClass {} // src/PrimaryTopic/SubTopic/SubSubTopic/Child/ChildClass1.php namespace PrimaryTopic\SubTopic\SubSubTopic\Child; class ChildClass1 extends ..\ParentClass {} // src/PrimaryTopic/SubTopic/SubSubTopic/Child/ChildClass2.php namespace PrimaryTopic\SubTopic\SubSubTopic\Child; class ChildClass2 extends ..\ParentClass {}
Similar TutorialsCode: [Select] Main Object ( [Visitor] = Visitor Object ( [user_info] => Array ( [username] = user [user_id] = 1 ) ) [Room] = Room Object ( [info] = Array( [id] = 1 [name] = Lobby ) [messages] = Messages Object ( [messages] = Array ( [0] = Array() ) ) ) ) Is there a way to get the visitor info within the Messages Object? I'm building it such as $Main = new Main; $Main->Visitor = new Visitor; $Main->Room = new Room; $Main->Room->Messages = new Messages; so they're not static I know that I can pass the $this var through each but that duplicates the class each time and I'm 99% sure that's bad. Hello all, I have some piece of code that is nested like this $variable = 'This is a global argument'; function parentFunction($variable) { function childFunction() { echo 'Argument of the parent function is '.$GLOBALS['variable']; } childFunction(); } parentFunction(5); What I want to know is - Is there a way to access a variable from the parent function without passing arguments to the child function? (Something like how classes have parent::?). I don't want to use $GLOBALS because it might cause some variable collision, and I didn't want to pass arguments because incase I decide to change the arguments in the parent function I'd have to do it in the child function aswell. From my searching around in the Internet it seems like this is not possible, but if theres a slight chance that there might be something out there, i'm willing to give it a shot . Thanks in advance I have a class which has two general types of methods.
Those which are used by the core class, and shouldn't be overridden.
Those which are "used" by the methods described above, and these can be overridden and new methods can be added. They are all private or protected, and not public.
For those used by the core class, I use the "final" keyword to prevent.
I am not really versed in namespace, but this seems like where it might be used.
Could I get a couple of pointers where to start?
Thanks
hey guy I've just recently re-altered my autoloader class which works with namespaces but it also calls classes which aren't namespaced, like so:
$class = new myclass();instead of $class = new Class\Myclass;im wondering have i defeated the whole point of namespacing and if all my classes should actually be namespaced hope its not such a pointless question The classes with the name of the first query column are located one directory above the file making the query. Tried use aliases as well as trying to include the name space in the first column of the query but no luck. Thanks
$stmt->fetchAll(\PDO::FETCH_CLASS | \PDO::FETCH_CLASSTYPE);
I've only seen composer packages using vendor name and package name namespace. Is it possible to use deeper namespace such as the following? For instance, package: { "name": "notioncommotion/my-sub-namespace/my-package", "description": "MyPackage located in MySubNamespace.", ... } is used in some project: { "require": { "notioncommotion/my-sub-namespace/my-package": "^1.2" } } If not, what do you think were the reasons composer does not allow? Thanks I think I am close, but not sure if I am quite there. Am I doing this correctly? I have several entity classes which will use class type inheritance:
MyNamespace\MyApp\Vehicle\Vehicle #MyNamespace.MyApp.Vehicle.Vehicle.dcm.yml MyNamespace\MyApp\Vehicle\Vehicle: type: entity table: vehicle_table inheritanceType: JOINED discriminatorColumn: name: discriminator_column type: string discriminatorMap: vehicle: Vehicle groundVehicle: MyNamespace\MyApp\Vehicle\GroundVehicle\GroundVehicle airVehicle: MyNamespace\MyApp\Vehicle\AirVehicle\AirVehicle repositoryClass: Vehicle id: id: type: integer generator: strategy: AUTO fields: grandParentItem1: type: string grandParentItem2: type: string #MyNamespace.MyApp.Vehicle.AirVehicle.AirVehicle.dcm.yml MyNamespace\MyApp\Vehicle\AirVehicle\AirVehicle: type: entity table: air_vehicle extends: MyNamespace\MyApp\Vehicle\Vehicle inheritanceType: JOINED discriminatorMap: airVehicle: AirVehicle airplane: MyNamespace\MyApp\Vehicle\AirVehicle\Airplane\Airplane rocket: MyNamespace\MyApp\Vehicle\AirVehicle\Rocket\Rocket repositoryClass: AirVehicle id: id: associationKey: true fields: parentItem1: type: string parentItem2: type: string #MyNamespace.MyApp.Vehicle.AirVehicle.Rocket.Rocket.dcm.yml MyNamespace\MyApp\Vehicle\AirVehicle\Rocket\Rocket: extends: MyNamespace\MyApp\Vehicle\AirVehicle\AirVehicle type: entity table: rocket_table id: id: associationKey: true repositoryClass: Rocket fields: childItem1: type: string childItem2: type: string I haven't shown GroundVehicle or Train, Car, and Airplane, but they are almost identical to AirVehicle and Rocket, respectively.
I've discovered that the barebone entities need to be manually created prior to using orm:generate-entities else I get class not found errors, and have created them as shown below. [michael@devserver doctrine]$ find inheritance_src -name "*.php" -exec cat {} \; <?php namespace MyNamespace\MyApp\Vehicle\AirVehicle\Airplane; class Airplane extends \MyNamespace\MyApp\Vehicle\AirVehicle\AirVehicle{} <?php namespace MyNamespace\MyApp\Vehicle\AirVehicle\Rocket; class Rocket extends \MyNamespace\MyApp\Vehicle\AirVehicle\AirVehicle{} <?php namespace MyNamespace\MyApp\Vehicle\AirVehicle; abstract class AirVehicle extends \MyNamespace\MyApp\Vehicle\Vehicle{} <?php namespace MyNamespace\MyApp\Vehicle\GroundVehicle\Car; class Car extends \MyNamespace\MyApp\Vehicle\GroundVehicle\GroundVehicle{} <?php namespace MyNamespace\MyApp\Vehicle\GroundVehicle\Train; class Train extends \MyNamespace\MyApp\Vehicle\GroundVehicle\GroundVehicle{} <?php namespace MyNamespace\MyApp\Vehicle\GroundVehicle; abstract class GroundVehicle extends \MyNamespace\MyApp\Vehicle\Vehicle{} <?php namespace MyNamespace\MyApp\Vehicle; abstract class Vehicle{} I then am able to successfully create the entities using orm:generate-entities, schema using orm:schema-tool:create, and repositories using orm:generate-repositories. Does everything seem correct? A couple of issues which I have my doubts: Locating the classes in inheritance_src/MyNamespace/MyApp instead of inheritance_src/MyApp. I tried to do the later and modify my psr-4 autoloader to use "MyNamespace\\": "inheritance_src/", but could not get it working without first creating the entities, editing them to add the "abstract" and "extend" portions, and then moving them to one directory lower. Needing to manually create the barebone classes in the first place. My meta-data definitions includes "extends: MyNamespace\MyApp\Vehicle\Vehicle", so I would thought it would not be necessary. Locate both the entities and repositories in the same folder. For that matter, I will probably be locating my service in the same folder as well. It seems that most frameworks will locate all the entities in one folder (with of course subdirectories), repositories in another, services in another, etc. The way I am doing seems to be organized domain modules as described by https://mnapoli.fr/organizing-code-into-domain-modules/. Any compelling reasons to do it one way or the other?
Thank you I am stumped as to why this simple code will not work...anyone care to enlighten me? <? namespace test; class testClass { function go() { echo "hello world"; } } $x='testClass'; $t = new $x(); $t->go(); ?> Running that code gives me this error: Fatal error: Class 'testClass' not found in test.php on line 11 If I remove the namespace declaration it works fine...anyone? I am assuming doing the following is bad practice as the short name "Account" is both for the repository and account. Agree? Appears that use always takes precedent over namespace and just Account\User is always short for \NotionCommotion\Domain\Entity\Account\User and not \NotionCommotion\Api\Account\User. Still sees like it could easily result in confusion. namespace NotionCommotion\Domain\Repository\Account; use NotionCommotion\Domain\Entity\Account; class AccountRepository { public function getUser(string $mainKey, int $userId):?Account\User { // } } Would it be better to do something like the following? Interestingly, it worked fine on the below class but when I tried to do so on the above AccountRepository class, it resulted in AccountDir\User class not found error. Is there any logic reason why that might/should happen, or is it more likely I just have some error elsewhere? namespace NotionCommotion\Api\Account; use NotionCommotion\Domain\Entity\Account as AccountDir; class AccountService { public function read():AccountDir\Account { // } } I have here what I would refer to as doozy. I'm not sure anyone is going to be able to help as there is allot involved. The issue could be in a number of locations, I am using PHPUnit as a test suite (and this is how I have found the issue) and PHP5.4rc5. The problem could be in either, or it could be in my code. I am also using a cascading filesystem (explained here) and while I'm confident this isn't the issue. It has the potential to be involved I guess. If you need to see how this is implemented it is see he https://github.com/proem/proem/blob/develop/lib/Proem/Api/Autoloader.php#L108 I have narrowed the problem down to two classes: https://github.com/proem/proem/blob/develop/lib/Proem/Api/Chain.php & https://github.com/proem/proem/blob/develop/lib/Proem/Api/Proem.php The tests that test these classes a https://github.com/proem/proem/blob/develop/tests/lib/Proem/Tests/ChainTest.php & https://github.com/proem/proem/blob/develop/tests/lib/Proem/Tests/ProemTest.php As the code stands right now, everything is fine and all tests pass. However, I am about to start some more work on the Proem\Api\Proem class and it needs access to the Proem\Api\Chain class. To do this, I add the line "use Proem\Chain;", the code becomes: <?php /** * @namespace Proem\Api */ namespace Proem\Api; use Proem\Chain; /** * Proem\Api\Proem * * The Proem boostrap wrapper (eventually) */ class Proem { const VERSION = '0.1.0'; } As soon as I do this the tests fail with: Quote Fatal error: Cannot use Proem\Chain as Chain because the name is already in use in /home/thorpe/src/proem/lib/Proem/Api/Proem.php on line 32 If I alias the namespace ( "use Proem\Chain as C;" ) the tests pass again. The issue is I shouldn't have to use an alias here. from the manual: Quote Note: Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules. Writing this post I have come to realise that the issue is likely that both the Chain class and the Proem class are in the same namespace yet I am trying to force the Chain class to be loaded from Proem\Chain instead of Proem\Api\Chain (this is part of the cascading filesystem setup). Anyway, if anyone has some ideas I would be grateful if you could take a look. Otherwise, it looks like I might need to do some re-shuffling. Iv'e created a ticket for this bug if anyone wan't to chip in. See here HEy Guys Here is a snippet of an array: SimpleXMLElement Object ( [report1] => SimpleXMLElement Object ( [ReportHost] => Array ( => SimpleXMLElement Object ( [HostName] => 192.168.2.34 [startTime] => Thu Dec 17 13:58:49 2009 [stopTime] => Thu Dec 17 15:13:13 2009 [netbios_name] => (unknown) [mac_addr] => (unknown) [dns_name] => (unknown) [os_name] => (unknown) [num_ports] => 2 [num_lo] => 0 [num_med] => 0 [num_hi] => 0 [ReportItem] => Array which is printed using print_r($a) now how can i access [HostName] => 192.168.2.34 in the $a array?? Thanks in advance I am a part of an institue which has internal web pages. It also provides me with my web space with php servers. Now I know those internal web pages are available to the server. I want a code that can fetch those web pages and display it in my web space. And whatever forms i fill or links i click it should do those on internal page and again return the new web page. Hi guys, I have xml that looks like: <item> <title></title> <link></link> <description></description> <dc:date>2011-02-27T16:42:31-05:00</dc:date> </item> I can access title, link, and description just fine. But how do I access a tag that has a colon? I tried this: Code: [Select] $x = new SimpleXmlElement($content); $entry = $x->item; $entry->children('dc', true)->date; Thanks for looking. LR If $result contains the result of mysql_query, a select count distinct query, how do I access the count? I've tried $result[0] to no avail and $result only returns the resource #. Hey everyone, I'm creating a website which requires users to login. I didn't want to have to do my own crypto, so I basically created it with Wordpress. I'm at the point where I don't want to use Wordpress for the whole site--just the login system. So I was wondering how I can piggyback off the user management system of Wordpress while using my own script for the rest of the site. I know that Wordpress ends up storing passwords in the database as HMACs, for what that's worth. Thank you, Ryan How do I access an object on different pages? I created object "A" on pageone.php and I need to access it on pagetwo.php. How do I do that? TomTees So basically I'm using PHP to solve a math problem for me. The user puts in a few parameters and the program runs those numbers through the algorithm and spits out a bunch of xy coordinates. I'm using the PHP SELF method to retrieve the user input. This all works fine, but it's afterwords that I run into problems. I create a drop down menu of all the x values and I want the user to be able to choose any x value they wish and to have the corresponding y value be displayed. The problem is that I have to use another POST command for this and when I do, it wipes out all the computed data. Of course the y-value is never displayed, and there lies the problem. I'm sure there are many ways to get around this, but I could not find one out myself. Anyone have any ideas? Hi all, I've tried to access the admin.php of the xcart installation he http://www.cleaningshopuk.com but it doesn't allow me to access it, saying that it's a 404 page not found error. That page does in fact exist, but I think the issue might be something to do with a redirection. The site owner apparently clicked on something in the admin section (possibly https or something similar) but what can I do to avoid this issue without having to re install everything? Thanks, Neil Can somebody help me what i'm doing wrong? The value from database is not visible when using: <?php $artikel = new Artikel(); $content= $artikel->printArtikel(); echo $content; ?> <?php class Artikel { // db connection protected $mydb; // database value protected $land; public function __construct($artikelnummer = false) { // databaseconnection: $this->mydb = new MyDB(); if($artikelnummer) { $this->load($artikelnummer); } else // load everything from database. { $sql = "SELECT * FROM artikel"; $this->mydb->doQuery($sql); $this->mydb->close(); } } public function load($artikelnummer) { $sql = "SELECT * FROM artikel"; $this->mydb->doQuery($sql); if($artikel = $this->mydb->fetch()) { // i got a feeling there is something missing here, like query from database?? } $this->mydb->close(); } public function printArtikel() { // return database value as table. $html = "<table border='1'>"; $html .= "<tr> <td>".$this->land."</td> </tr>"; $html .= "</table>"; return $html; } } ?> Other class are accessing extern. For example database connection with class MyDB. please, can somebody correct my script?? Hello All: I am trying to access a value within an object. The value name has whitespace in it and is called "zip code". When I try to access the value via regular object call such as: Code: [Select] $sub = $sub->zip code; PHP goes crazy on me. I've tried to encapsulate the value name within a variable like this: Code: [Select] $zip_code = "Zip Code"; $sub = $sub->zip_code; and that doesn't work either. Does anyone have any ideas about how I should go about yanking the data out? |