I have a function that gets a class passed to it as a parameter. I would like to get the class name of the passed class as a string.
I tried putting this method in the passed class:
function getClassName()
{
return __CLASS__;
}
but if the class is extended I assumed this would return the name of the subclass but it still returns the name of the super class which I find kind of odd.
So given a $var passed to a function as a parameter, is there a way to get a string of the class name?
Thanks!!
See get_class, that should be exactly what you're trying to achieve.
$class_name = get_class($object);
Simplest way how to get Class name without namespace is:
$class = explode('\\', get_called_class());
echo end($class);
Or with preg_replace
echo preg_replace('/.*([\w]+)$/U', '$1', get_called_class());
__ CLASS __ with return the name of the class the method is implemented in.
If you want to get the class name of an object passed then you can use:
get_class($param);
Also, if you're using PHP5 then the Reflection classes provided are also useful.
Use get_class:
$className = get_class($object);
Straight from the php docs: http://uk.php.net/manual/en/function.get-class.php
<?php
abstract class bar {
public function __construct()
{
var_dump(get_class($this));
var_dump(get_class());
}
}
class foo extends bar {
}
new foo;
?>
The above example will output:
string(3) "foo"
string(3) "bar"
you could also add a method into the passed class(es) returning this:
var_dump(get_called_class());
Related
Can I use PHP's \ReflectionClass without explicitly setting the class name?
My aim is to get class information inside its own context.
Instead of:
class Test {
public function getSomeClassInfo() {
$reflection = new \ReflectionClass('Test');
// ...
}
}
I want to do:
class Test {
public function getSomeClassInfo() {
$reflection = new \ReflectionClass({$this});
// ...
}
}
As the documentation of PHPs ReflextionClass shows the class constructor accepts two alternate arguments: either a class name or an object. Since $this is a pointer to an object I'd say that should be possible. If not (give it a test!), then just use the generic class name constant __CLASS__ instead.
I'm just learning the php and came out a question in my mind, Can I define the class within the function like this:
public class test{
public function newtest(){
// defining a class here like this:
public class funclass{
.....
}
}
}
Yes, you can
function a(){
class A {
}
}
var_dump(class_exists('A')); //bool(false)
a();
var_dump(class_exists('A')); //bool(true)
But, remeber that classes are globals. You cannot bound class to function scope only.
You can not.
Run your code after removing those publics and you ll get this:
Fatal error: Class declarations may not be nested on line 6
Read 1
Read 2
I am calling a function inside a class, function is outside the class. I want to pass $this of the class to that function,
currently I am doing this:
class foo {
func_outside_class($this);
}
function func_outside_class($context){
return $context;
}
Is there a way so that I don't need to pass context as function parameter.
Or my above code is OK?
I cannot create an object inside function, cause this class need some parameters.
#Barmar wrote:
There's nothing wrong with passing $this to an outside function. But I don't understand why you're immediately overwriting the $context variable -- why does the function take an argument if it's not using it? Perhaps you mean $this = $context.
If that's that case, I don't think you can do it. You're not allowed to assign $this, it's a special variable that always contains the current object in a class method.
In the outside function, you can simply do $context->method(...) to use it instead.
If you're trying to just reference methods inside of you class you can do something like this
class Foo {
public function bar() {
return 'Hello World';
}
}
function func_outside_class() {
// instantiate class
$foo = new Foo;
// call method bar() from Foo class
echo $foo->bar();
}
Anyone know how to redeclare function?
For example:
class Name{}
//Code here
class Name{}
And the output is:
Cannot redeclare class
So I tried to name the class as a variable:
$foo = 'name';
class $foo{}
//Code
class $foo{}
Edit: I have a DataBase table and I am reading data from the table user 'while()' loop.
I have a class that uses the table and echo some information.
That is the reason.
And it's still not working...
Any suggestions?
If you want to have several classes with the same name, then you should be using namespaces.
Use PHP NameSpace
namespace A {
class Name {
function __toString() {
return __METHOD__;
}
}
}
namespace B {
class Name {
function __toString() {
return __METHOD__;
}
}
}
namespace C {
echo new \A\Name ,PHP_EOL;
echo new \B\Name ,PHP_EOL;
}
Output
A\Name::__toString
B\Name::__toString
Nope. PHP simply doesn't support runtime modification of an existing class
Why would you want to re-declare a class? when you ca reference to it at any point?
I think what you're looking for is to create a new instance of a class:
$foo = 'name';
$obj1 = new $foo();
//Code
$obj2 = new $foo();
You shouldn't have 2 or more classes with the same name. This is not working. How should the interpreter know which class is the correct class?
Change the name of the second class and extend them if you need it.
Edit: Use Namespaces of you need the same classname or if you have an existing class.
If you need two instances of a class you can just initialise it twice as two different varialbes;
class Foo{}
$foo1 = new Foo;
$foo2 = new Foo;
Use PHP namespaces to declare classes with the same name but different purposes.
First of all: Do not do this.
That said, if you really need to "refedine" a class, you can use a wrapper class to do so
<?php
class foo { //Original definition here };
class bar {
var $baseobj;
function bar($some, $args) {
$this->baseobj=new $which($some, $args);
}
function someMethod() {
return $this->baseobj->someMthod();
}
//...
}
class baz {
function someMethod() {
return "This is the result";
}
//...
}
$obj=new bar($this, $that);
//$obj is now a wrapped foo object
echo $obj->someMethod(); //Will call into foo
$obj->baseobj=new baz($this, $that);
//$obj is now a wrapped baz object
echo $obj->someMethod(); //Will call into baz
?>
Again: Do not do this without a very, very good reason!
Disclaimer: This implementation is more than crude, it is only ment to transport the idea.
class Second
{
// i've got to access to $variable from First instance from here
}
class First
{
public $variable;
public $SecondInstance;
public function __construct($variable)
{
$this->variable = $variable;
$this->SecondInstance = new Second();
}
}
$FirstObj = new First('example variable');
I need an equivalent for parent::$variable for objects.
Is there a possibility to do that in that way?
No, you cannot. The only way you can manage that, without extending First, is to pass "$this" to the constructor of Second:
$this->SecondInstance = new Second ($this);
Or, you can simply pass $variable to its constructor.
You mean like the parent function in PHP:
//You may find yourself writing code that refers to variables
//and functions in base classes. This is particularly true if
// your derived class is a refinement or specialisation of
//code in your base class.
//Instead of using the literal name of the base class in your
//code, you should be using the special name parent, which refers
//to the name of your base class as given in the extends declaration
//of your class. By doing this, you avoid using the name of your base
//class in more than one place. Should your inheritance tree change
//during implementation, the change is easily made by simply
//changing the extends declaration of your class.
<?php
class A {
function example() {
echo "I am A::example() and provide basic functionality.<br />\n";
}
}
class B extends A {
function example() {
echo "I am B::example() and provide additional functionality.<br />\n";
parent::example();
}
}
$b = new B;
// This will call B::example(), which will in turn call A::example().
$b->example();
?>
I would recommend you change your strucutre a little to "extend":
class second extends first{
public __construct(){
parent::__construct();
echo $this->variable;
}
}
Otherwise you will need to assign the "first" as a parent class within the variables on the second and actually access it like that:
class second{
public $first;
public function __construct($first){
$this->first = $first;
var_dump($this->first->variable);
}
}
Or of course you can also make the first class static and access it that way.