Page 1 of 1

Late static binding - syntax error

Posted: Fri Aug 22, 2014 3:05 pm
by agardo
Hello Everyone!
I have the following simple code:

Code: Select all

<?php
class A {
    public static function name()
    {
        echo get_called_class();
    }
}

class B extends A {
}

A::name();
B::name();

?>
The code returns AB is OK, however I would like to catch get_called_class() in a variable and instantiate the classes:
var $class_name = get_called_class();
$object = new $class_name;
I am using Dreamweaver CS6 and I get an syntax error on this: var $class_name = get_called_class();
What is more stranger that get_called_class() does not show up in the code completion but I checked that the function exist:
function_exists(get_called_class); returns true;
Why i cannot do this?: var $class_name = get_called_class(); // returns a string as i know
If I could, my code would have been simplified a lot.
Somebody could help on this?

Re: Late static binding - syntax error

Posted: Fri Aug 22, 2014 3:22 pm
by requinix
The syntax error is on "var". That's only used for class variables, and only for PHP 4.

Code: Select all

public static function name()
{
	$class_name = get_called_class();
	$object = new $class_name;

Re: Late static binding - syntax error

Posted: Fri Aug 22, 2014 3:36 pm
by agardo
Thank for helping.
I am following the PHP MYSQL beyond the basics with Kevin S. and I wanted to export all abstracted classes from users,photographs, comments into a DatabaseObject superclass.
Anyway thanx for help!