note 85267 added to language.oop5.static
| From: | nacion_01 at hotmail dot com | Date: | Fri, 22 Aug 2008 16:38:15 +0000 |
| Subject: | note 85267 added to language.oop5.static | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143754@lists.php.net to get a copy of this message | ||
Thanks to all of you, this is my little contribution, i hope it give you ideas:
The next script uses a class Logs to store and show the logs. You can show the last logs of your
scripts. You can:
- push ("a new log")
- show () // the last logs until the las call to show()
- showAll() // to show all the logs and
- message("a forced message") // which sends a message to the log output with out storing
the message (you may imagine other solutions)
<?php
class Logs{
// this makes this thing to be hidden from outside calls [private]
private static $logs=array("start");
private static $index=0;
// this is available for outside calls [public]
public static function getLogsCount(){
// the constant "self" refers to the class, instead $this wich
// refers to an instance of a class (this class is not instanced,
// its running itself, so lets use "self" and "::" to access the
// "static" functions and properties.
$count=count(self::$logs);
return $count;
}
public static function show(){
//echo ("# Logs::show ".self::getLogsCount());
$count=self::getLogsCount();
// start the show since the last log shown ($index)
for ($i=self::$index; $i<$count;$i++){
$log=self::$logs[$i];
self::message($log);
}
// update the las log shown index
self::$index=$i;
}
public static function showAll(){
//echo ("# Logs::show ".self::getLogsCount());
// show the logs from cero to last
// im starting with php so I use for (...) because its my
// way in AS3.0 but you better keep checking that
$count=self::getLogsCount();
for ($i=0; $i<$count;$i++){
$log=self::$logs[$i];
self::message($log);
}
}
// Yup! a new log!
public static function push($msg){
//self::message("# Logs::push ");
array_push(self::$logs, $msg);
}
// Send a message to the html output.
// Now, a little words about this one, you may want to use an
// special button on your html or somewhere else to show or hide the
// logs, because you are in development and want to keep this thing
// little hidden of your clients. Probably may generate a java script
// variable so instead of [echo "<p>...</p>";] is ok to
// use [echo "<script>logs+=$msg</script>"]
// I dont know, figure it out.
public function message($msg){
echo "<p>- Logs > $msg </p>";
}
}
// Ready to use.
// to call a function in an static class, refer to the function as
// TheClass::theFunction
// remember, php uses :: (sorry, I still learning why because I come from the AS3 world)
Logs::push("log1");
Logs::show();
Logs::push("log2");
Logs::show();
Logs::push("log3");
Logs::showAll();
?>
----
Server IP: 69.147.83.197
Probable Submitter: 189.137.48.213
----
Manual Page -- http://www.php.net/manual/en/language.oop5.static.php
Edit -- https://master.php.net/note/edit/85267
Del: integrated -- https://master.php.net/note/delete/85267/integrated
Del: useless -- https://master.php.net/note/delete/85267/useless
Del: bad code -- https://master.php.net/note/delete/85267/bad+code
Del: spam -- https://master.php.net/note/delete/85267/spam
Del: non-english -- https://master.php.net/note/delete/85267/non-english
Del: in docs -- https://master.php.net/note/delete/85267/in+docs
Del: other reasons-- https://master.php.net/note/delete/85267
Reject -- https://master.php.net/note/reject/85267
Search -- https://master.php.net/manage/user-notes.php