note 51313 modified in function.eval by danbrown

From: Date: Sun, 17 May 2009 20:00:14 +0000
Subject: note 51313 modified in function.eval by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155306@lists.php.net to get a copy of this message
The eval function becomes incredibly useful when dealing with static class members and variables. For instance: Lets say you have 3 classes; Foo, BarA and BarB, where BarA and BarB are children of Foo. Now lets also say that both BarA and BarB contain a static member function called getDataSource(). To call getDataSource() you would have to use the syntax: <?php BarA::getDataSource(); BarB::getDataSource(); ?> But lets say you need to access getDataSource() from inside class Foo during an instance of either BarA or BarB. You can use eval to do something like this: <?php eval('$dataSource=' . get_class($this) . '::getDataSource();'); ?> --was-- The eval function becomes incredibly useful when dealing with static class members and variables. For instance: Lets say you have 3 classes; Foo, BarA and BarB, where BarA and BarB are children of Foo. Now lets also say that both BarA and BarB contain a static member function called getDataSource(). To call getDataSource() you would have to use the syntax: BarA::getDataSource(); BarB::getDataSource(); But lets say you need to access getDataSource() from inside class Foo during an instance of either BarA or BarB. You can use eval to do something like this: eval('$dataSource=' . get_class($this) . '::getDataSource();'); http://php.net/manual/en/function.eval.php

« previous php.notes (#155306) next »