Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you're looking for the <a href="http://ca.php.net/include_once" rel="nofollow"><code>include_once</code></a> 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 <code>include_once("thatfile.php");</code> to make that class accessible to your application.</p> <p>I should clarify that you should use <code>include_once</code> (or <a href="http://ca.php.net/require_once" rel="nofollow"><code>require_once</code></a> if you would prefer) over <code>include</code> because if you include your class file twice, you'll get errors saying you're trying to define a class that already exists.</p> <p>Using your example, your program would look like this:</p> <p><strong>MessageClass.php</strong></p> <pre><code>&lt;?php class Message { public $message; // Return a string containing a list of messages found public function listMessages($delim = ' '){ if (count($this-&gt;message) &gt; 0){ echo implode($delim,$this-&gt;message); }else{ return false; } } // Manually add Message public function addMessage($description){ $this-&gt;message[] = $description; } } </code></pre> <p><strong>index.php</strong></p> <pre><code>&lt;?php include_once("MessageClass.php"); $message = new Message(); $message-&gt;addMessage('Article Title 1'); $message-&gt;addMessage('Article Title 2'); $message-&gt;listMessages(); </code></pre> <p>Also notice that I changed your class variables (members) and functions (methods) to include the <code>public</code> modifier. I'm not 100% sure about how it works in PHP, but in most languages these things tend to be <code>private</code> by default, meaning they can't be accessed from outside of the class. It's always better to include them to be clear.</p> <p>You might also want to look into the <a href="http://ca.php.net/__autoload" rel="nofollow"><code>__autoload()</code></a> 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 <code>include_once</code> it automatically when you need it. More generally, you should look into the <a href="http://ca.php.net/manual/en/function.spl-autoload-register.php" rel="nofollow"><code>spl_autoload_register</code></a> function, which is the same thing but with a lot more flexibility.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload