LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-22-2005, 06:21 AM   #1
Boby
Member
 
Registered: Feb 2004
Posts: 781

Rep: Reputation: Disabled
PHP and classes


Hello!

I have to write some php code and use classes. One class is for formating text and one for output. These are some simplyfied examples:
PHP Code:
<?php
class Operations
{
   function 
format_result($string)
   {
      
// Remove HTML and PHP tags
      
$string strip_tags$string );
      
// Remove "\r\n", "\r" and "\n" with a space
      
$string str_replace"\r\n" " "$string ); // Windows
      
$string str_replace"\r"   " "$string ); // Mac OS
      
$string str_replace"\n" " "$string ); // Unix
      // Remove tabs "\t"
      
$string str_replace"\t" " "$string );
      
// Remove multiple spaces
      
$string preg_replace"/\s{2,}/"" "$string );

      return 
$string;
   }

   function 
highlight_keyword($string$keyword)
   {
     
$string preg_replace("#(".$keyword.")#si"'<span class="highlight">\1</span>'$string);

     return 
$string;
   }
//end class

class output
{
   var 
$string "";
   var 
$keyword "";
   var 
$job;

   function 
format()
   {
      
$this->job = new Operations;
   }

   function 
import($string$keyword)
   {
      
$this->string $string;
      
$this->keyword $keyword;
   }

   function 
make_output()
   {
      echo 
"<div>".$this->job->format_result($string)."<br />".$this->job->highlight_keyword($string$keyword)."</div>";
   }
//end class

$text "  This is a random    text for this     thread.  Text is now ready";
$word "text";

$class = new output();
$class->import($text$word);
$class->format();
$class->make_output()
?>
The problem is it doesn't work because it won't use the class "Operations" in the class "output". How do I use the first class in the second one? I've read some documentation of PHP but I couldn't get it work. It would be very nice to have some code examples from you.

Thank you!
Regards, Boby!
 
Old 03-22-2005, 06:30 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
A way to fix it :
PHP Code:
function make_output()
   {
      
$job $this->job;
      echo 
"<div>".$job->format_result($string)."<br />".$job->highlight_keyword($string$keyword)."</div>";
   } 
Another way :
PHP Code:
class output extends Operations {
...
function 
make_output()
   {
      echo 
"<div>".$this->format_result($string)."<br />".$this->highlight_keyword($string$keyword)."</div>";
   } 
another way :
PHP Code:
function make_output()
   {
      echo 
"<div>".Operations::format_result($string)."<br />".Operations::highlight_keyword($string$keyword)."</div>";
   } 
 
Old 03-22-2005, 10:29 AM   #3
Boby
Member
 
Registered: Feb 2004
Posts: 781

Original Poster
Rep: Reputation: Disabled
Thank you very much keefaz!
You are great!

I am using now your last choice:
PHP Code:
function make_output()
   {
      echo 
"<div>".Operations::format_result($string)."<br />".Operations::highlight_keyword($string$keyword)."</div>";
   } 
This looks the best for me!

Thank you again!
Cheers, Boby!
 
Old 03-22-2005, 11:14 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
The third choice is possible here because Operations is a static class (no use of $this)
If you use it, you could remove
PHP Code:
function format()
   {
      
$this->job = new Operations;
   } 
...as it is not longer needed
 
Old 03-23-2005, 01:49 AM   #5
Boby
Member
 
Registered: Feb 2004
Posts: 781

Original Poster
Rep: Reputation: Disabled
So if I use $this->function or $this->variable in the first class I won't be able to use the above example choosed by me and I have to use the one with "class foo extends bar"?

If not when do I use this example or the other example?

Thank you!
Boby
 
Old 03-23-2005, 05:15 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Yes, you can't use $this on a static class
But you don't need absolutely do an extends, you can follow the
first example and keep your format() function that initialize
Operations class
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes in VC++ Diederick Programming 2 11-30-2005 10:57 AM
Possible uses for classes. RHLinuxGUY Programming 4 11-21-2005 10:10 PM
classes in C++ niteshadw Programming 2 07-05-2005 07:05 PM
php classes, arrays towlie Programming 10 06-07-2005 11:10 AM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration