PHP - Get_class($this) With Namespaces
when trying to call
get_class($this)it will return the namespace of my class Application\Models\User_Model But what im really after is just User_Model... is there a way of actually getting the class name and not the namespace itself? thought i'd ask before making a class for that...thank you Similar TutorialsHi Guys, still new to OOP. What I'm trying to achieve is quite straightforward and I've had some success, however, it feels "wrong" how I'm implementing the classes I have created which makes me feel I have a design problem or there is a better way to achieve what I want which right now I cannot see.
The task is quite clear, I receive a numerical user_id, after setting up the DB connection I pass it to a factory which executes a few queries and then generates an array of objects, the array will hold the objects relevant to what the person has been, i.e. has been a player, has been a coach, referee or whatever (when I pass the id I obviously have no idea of the role a person has held, it may be player only, it could be player and coach, etc).
My person class is extended to specific roles, player, referee, coach etc and obviously the array of returned objects enables me to access the properties required.
The issue is during implementation I obviously need to know what objects I have as to what methods I can call. So attempting $my_person->refGame will throw an error if $my_person is only holding a player object.
So my imlementation is along the lines of this the moment..
$my_person= PersonFactory::createPerson($database, $person_id); // my_person can be an array of objects, getName from parent class valid for all objects $details = $my_person[0]->getName(); // We need to know what class we have returned to call specific methods... foreach($my_person as $dis_array){ if (get_class($dis_array) == 'Player'){echo "I've been a player"; $my_person[0]->playerMethod; etc} if (get_class($dis_array) == 'Manager'){echo "I've been a manager"; $my_person[1]->somemanagerMethod; etc} if (get_class($dis_array) == 'Referee'){echo "I've been a Ref"; $my_person[2]->refMethod} }Really using the if block to identify the classes returned feels wrong. I feel like I'm missing something here, how should I be handling a returned array of objects in implementation? Also accessing each object via $my_person[0] (player) or $my_person[1] (manager) is wrong in implementation. Any help much appreciated. Edited by mich2004, 29 May 2014 - 07:23 AM. Hey Guys. I am new to using namespaces. I am trying to use it on the code below but it keeps giving me an error message saying Class 'lesson_one\Lecture' not found
I am not sure what I am doing wrong here... I have provided an example below. I know there is a duplicate conditional statement. But I am just working with this code to test out the namespace.
Any help would be really appreciated!
namespace lesson_one; abstract class Lesson { protected $duration; const FIXED = 1; const TIMED = 2; private $costtype; function __construct($duration, $costtype=1) { $this->duration = $duration; $this->costtype = $costtype; } function cost(){ switch($this->costtype) { case self::TIMED: return (5* $this->duration); break; case self::FIXED: return 30; break; default: $this->costtype = self::FIXED; return 30; } } function chargeType() { switch($this->costtype) { case SELF::TIMED: return "hourly rate"; break; case self::FIXED : return "fixed rate"; break; default: $this->costtype = self::FIXED; return "fixed rate"; } } } class Lecture extends Lesson { } class Seminar extends Lesson { } //echo Lesson::FIXED; $lecture = new \lesson_one\Lecture("5", Lesson1::FIXED); echo $lecture->cost(). "<br>"; echo $lecture->chargeType(). "<br>"; Does the concept of a "Namespace" exist in PHP? If so, how do you use them? This is a snippet from ASP.NET that made me ask this question... Quote <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> TomTees
I have to be doing something wrong here, but so far Googling hasn't helped. Take a look he
https://demo.envrin.com/api/classes/apex.app.msg.emailer.html#method_send
The doc-block is correct, or at least as far as I can tell it is, character-by-character. I'm blind though, but have tried different combinations of things like "String" instead of "string" or "@Param" instead of "@param". Made sure the ends of the @param lines ende din a period, and that seemed to help a little.
Nonetheless, why does it keep adding the namespace to everything in front of "string" like that? Any why are the paramaters section all screwed up?
Any help would be appreciated. Thanks!
Hi,
Well, I'm having this
The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'GetSomething'. End element 'Body' from namespace 'http://schemas.xmlso.../soap/envelope/' expected. Found element 'param1' from namespace ''.
The soapclient request itself is something with multiple namespaces, like :
<aaa xmlns="namespace1"> <bbb xmlns="namespace2" xmlns:i="http://www.w3.org/20...hema-instance"> <requestparams>xxxxx</requestparams> </bbb> </aaa> So far I think this may got to do with multiple namespaces, so I've been trying to figure out how to deal with this for hours to no avail. Anyone can shed a light into this problem ? Thanks in advance, I'm not entirely sure what is wrong with me, but I'm having a super difficult time understanding namespaces.
I have 2 primary classes
# Resides within class.DataTypeBase.php namespace DTB\Base; class DataTypeBase { protected $name = ''; protected function getName() { return $this->name; } protected function setName($name) { $this->name = $name; } } # Resides within class.DataType.php namespace DTB; class DataType extends DataTypeBase { public function process_data(){ .... } }I then have a third file that contains approximately 9 different classes that further extend DataType # Resides within class.DataTypes.php namespace DataTypes; class Silver extends DataType { public function differ($type) { .... } }The namespace system seems to work for class DataTypes, but with the class Silver I get an error message saying that the class DataType does not exist. From what I read online, and using the example above I figured the "extends DataType" may have to be "extends \DTB\DataType" but it results in the same error message. I'm not entirely sure what I need to do for this to work properly |