tags:

views:

59

answers:

3

as follow:

<?php
/*
 * @I'm data
 */
function demo() {}

how to get "I'm data"? thx

+2  A: 

Well, if you are accessing it via the demo() function...

// @I'm Data
function demo(){

    $script = file(__FILE__);
    $comment = $script[__LINE__ - 5]; // 4 lines above, and 1 for arrays
    $temp = explode("@", $comment);
    return $temp[1];
}
Chacha102
It blinks "code smells".
Luc M
@Luc Yes, it does. But so does trying to access the comment at all.
Chacha102
Your response is very good. But why someone would like to do that. Imagine working on that file and you add/remove a comment line. You think that it doesn't change anything to the result. It can be very hard to find a bug because a comment line has been modified. I'm very curious why someone would like to do that. I comment your response because it has been accepted. The comment was targeted to guy who accepted it. Why, Why Why does he need it ?
Luc M
@Luc I do some configurations by that, the idea comes from some java frameworks, but I have given up beacause of modifying har
tq0fqeu
WordPress uses this method as a means of recognizing authors who create templates and plugins. example: `/* Author: Russell Dias. Version: 1.1.1 */`
Russell Dias
@Chacha102: your follow up comment *"Yes, it does. But so does trying to access the comment at all."* deserves to be in your original answer, IMO.
Bart Kiers
A: 

There's no obvious way to do it -- your script is blissfully unaware of its own comments.

However, you could probably hack it by having your script read itself as data, and then parse out whatever you're looking for:

<?php
$my_own_source = file_get_contents(__FILE__);

//some code to pull out exactly what you want here.
timdev
A: 

If your code is inside a class, the correct way is to use reflection:

http://www.php.net/manual/en/reflectionclass.getdoccomment.php

Foo