I'm having troubles with this classes in PHP...
What I want is a "notification" system across my whole website - however this script only works when everything is in one file. How can I fix this.
class Message {
var $message; // A variable to store a list of messages
// Return a string containing a list of messages found,
function listMessages($delim = ' '){
if (count($this->开发者_运维技巧message) > 0){
echo implode($delim,$this->message);
}else{
return false;
}
}
// Manually add Message
function addMessage($description){
$this->message[] = $description;
}
}
$message = new Message();
$message->addMessage('Article Title 1');
$message->addMessage('Article Title 2');
$message->listMessages();
I think you're looking for the include_once
function. Essentially, you want to have each of your classes in their own separate file, and then when you want to use them, you call include_once("thatfile.php");
to make that class accessible to your application.
I should clarify that you should use include_once
(or require_once
if you would prefer) over include
because if you include your class file twice, you'll get errors saying you're trying to define a class that already exists.
Using your example, your program would look like this:
MessageClass.php
<?php
class Message {
public $message;
// Return a string containing a list of messages found
public function listMessages($delim = ' '){
if (count($this->message) > 0){
echo implode($delim,$this->message);
}else{
return false;
}
}
// Manually add Message
public function addMessage($description){
$this->message[] = $description;
}
}
index.php
<?php
include_once("MessageClass.php");
$message = new Message();
$message->addMessage('Article Title 1'); $message->addMessage('Article Title 2');
$message->listMessages();
Also notice that I changed your class variables (members) and functions (methods) to include the public
modifier. I'm not 100% sure about how it works in PHP, but in most languages these things tend to be private
by default, meaning they can't be accessed from outside of the class. It's always better to include them to be clear.
You might also want to look into the __autoload()
function, which is called whenever you try to use a class which hasn't yet been defined. In this function, which you define yourself, you can figure out in which file a given class resides, and include_once
it automatically when you need it. More generally, you should look into the spl_autoload_register
function, which is the same thing but with a lot more flexibility.
If you want these messages to be viewable across all pages on your site, then you need to store them somewhere permanent. Anything you put in a PHP variable ceases to exist after each page request. Put the messages in a file or a database, and read them from the database when you instantiate your Message object.
you can create one class file where this class are situated with its all methods then you have to include this class file in the all file where you want to use this class and methods. and then you can use this class with the create an object.
Thanks.
In PHP, multiple files can be made to work together using include
/require
and their cousins include_once
/require_once
. Let me demonstrate by example. You can break things up into multiple files as follows; Put the class in a file called Message.php
which might look like this:
<?php
class Message {
var $message; // A variable to store a list of messages
// Return a string containing a list of messages found,
function listMessages($delim = ' '){
if (count($this->message) > 0){
echo implode($delim,$this->message);
}else{
return false;
}
}
// Manually add Message
function addMessage($description){
$this->message[] = $description;
}
}
?>
And include it in another file, in the same directory called notification.php
which might look like this.
<?php
require_once 'Message.php';
$message = new Message();
$message->addMessage('Article Title 1');
$message->addMessage('Article Title 2');
$message->listMessages();
?>
Have you tried putting your class into a seperate file
-- class.Message.php -----
var $message; // A variable to store a list of messages
// Return a string containing a list of messages found,
function listMessages($delim = ' '){
if (count($this->message) > 0){
echo implode($delim,$this->message);
}else{
return false;
}
}
// Manually add Message
function addMessage($description){
$this->message[] = $description;
}
}
?>
---- end class -----
--- testPageMessage.php -----
<?php
require_once('class.Message.php');
$message = new Message();
$message->addMessage('Article Title 1');
$message->addMessage('Article Title 2');
$message->listMessages();
?>
---- end test page -----
On a side note you may want require or include. The difference being: REQUIRE will throw an exception if not found. INCLUDE will throw a warning if not found.
The _ONCE (Require_once or Include_once) will make sure the file is only included once. (as opposed to multiple times)
Although, its not entirely clear from your question that the following solution may be of use, but try calling the last four lines in the footer
file (which is included in every page), if you've it..
$message = new Message();
$message->addMessage('Article Title 1');
$message->addMessage('Article Title 2');
$message->listMessages();
精彩评论