Page 1 of 1

Echo Minimal Class

Posted: Mon Mar 10, 2008 12:20 pm
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');
?>

Re: Echo Minimal Class

Posted: Mon Mar 10, 2008 1:04 pm
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.

Re: Echo Minimal Class

Posted: Mon Mar 10, 2008 1:19 pm
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.

Re: Echo Minimal Class

Posted: Mon Mar 10, 2008 1:40 pm
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"

Re: Echo Minimal Class

Posted: Mon Mar 10, 2008 1:46 pm
by JAB Creations

Code: Select all

<?php
class error {
public function __toString() {return '1';}
}
$error = new error();
echo $error;
?>

Re: Echo Minimal Class

Posted: Mon Mar 10, 2008 2:47 pm
by anto91
ehm, what was the point of your post?

Re: Echo Minimal Class

Posted: Mon Mar 10, 2008 3:00 pm
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.

Re: Echo Minimal Class

Posted: Mon Mar 10, 2008 6:41 pm
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.

Re: Echo Minimal Class

Posted: Mon Mar 10, 2008 7:11 pm
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.

Re: Echo Minimal Class

Posted: Mon Mar 10, 2008 7:48 pm
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.

Re: Echo Minimal Class

Posted: Mon Mar 10, 2008 7:59 pm
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?

Re: Echo Minimal Class

Posted: Mon Mar 10, 2008 8:24 pm
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."