Echo Minimal Class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Echo Minimal Class

Post by JAB Creations »

I'd like to create a class and echo it, that's it!

What I would like to do is create an 'error' class that I can use in my templating system to enable or disable error messages. I'd like to be able to simply echo the class. What code can I strip from this and still have the page echo '1' as the value?

Code: Select all

<?php
class error {
private $query;
public function set($name,$value) {$this->$name = $value;}
public function get($name){return $this->$name;}
}
 
$error = new error();
$error->set('query',"1");
echo $error->get('query');
?>
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: Echo Minimal Class

Post by anto91 »

JAB Creations wrote:I'd like to create a class and echo it, that's it!

What I would like to do is create an 'error' class that I can use in my templating system to enable or disable error messages. I'd like to be able to simply echo the class. What code can I strip from this and still have the page echo '1' as the value?

Code: Select all

<?php
class error {
private $query;
public function set($name,$value) {$this->$name = $value;}
public function get($name){return $this->$name;}
}
 
$error = new error();
$error->set('query',"1");
echo $error->get('query');
?>
All you need to do is implement the __toString()

Example

Code: Select all

 
class Error {
public function __toString() {
    return 'Hello world';
}
}
 
But i realy think you should reconsider the structure of your class since toString does not take any parameters.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Echo Minimal Class

Post by JAB Creations »

What? I'm trying to simplify things; I have no idea how to echo that class/subclass echo ($class->get('subclass');).

Also I need to echo this in another file which is the point of using classes. If I can echo PHP or alert JavaScript I know I'm able to access it.
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: Echo Minimal Class

Post by anto91 »

Ehm you said it your self echo the class

Code: Select all

 
$error = new Error();
 
echo $error;
 
Using my code it would output "hello world"
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Echo Minimal Class

Post by JAB Creations »

Code: Select all

<?php
class error {
public function __toString() {return '1';}
}
$error = new error();
echo $error;
?>
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: Echo Minimal Class

Post by anto91 »

ehm, what was the point of your post?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Echo Minimal Class

Post by JAB Creations »

The thread of my post?

The thread to figure out what the minimal amount of code I need to be able to declare a class on one page and so that I could echo it's value on another.

The last post I made without comment was that that code worked though I'm not sure if it's yet minimal...which I should have clarified.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Echo Minimal Class

Post by Christopher »

I am a little confused too. The way that you "echo a class" is to implement the __toString() method so that the class can be used in a scalar context.
(#10850)
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Echo Minimal Class

Post by JAB Creations »

Classes exist to be used as universal variables, sort of like a super global but without the security concerns.

I'd want to declare a class and a value so that I could determine if the class exists in a different file.

I want to echo the class's value to ensure that it has actually been set.

If I can't echo the class on a second file I am less if at all likely to know if the desired behavior exists by accident or if it's working as desired because it's correctly written.

The return is within the class declaration and therefor is less then minimal code to echo the class's value on a second file.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Echo Minimal Class

Post by Christopher »

I am really not clear at all what you are talking about or want. I think you are mixing up objects and classes.
(#10850)
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Echo Minimal Class

Post by JAB Creations »

You can't use a variable across files right?

But you can use a class across files.

Code: Select all

$var = 'value';
That's the minimal amount of code to set a variable.

What is the minimal amount of code to set a class?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Echo Minimal Class

Post by Christopher »

JAB Creations wrote:You can't use a variable across files right?
Why not? You can declare a variable in one script and use it in another.
JAB Creations wrote:But you can use a class across files.
When you say "use" do you mean create and instance of it? Call it statically?
JAB Creations wrote:What is the minimal amount of code to set a class?
You can't "set a class." You can create an instance of a class. You can set a static property of a class. But you can't "set a class."
(#10850)
Post Reply