Late static binding - syntax error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
agardo
Forum Newbie
Posts: 2
Joined: Fri Aug 22, 2014 2:41 pm

Late static binding - syntax error

Post 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?
Last edited by agardo on Fri Aug 22, 2014 3:22 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Late static binding - syntax error

Post 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;
agardo
Forum Newbie
Posts: 2
Joined: Fri Aug 22, 2014 2:41 pm

Re: Late static binding - syntax error

Post 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!
Post Reply