Jump to content

[SOLVED] classes and objects


Cardale

Recommended Posts

I was reading over some documentation about classes and had a question.  When you put a object into a certain state it stays that way?  So the value of that state could be used again in another instance?  Like another script after the state has been set?

Link to comment
Share on other sites

So say for instance I had a function like this

 

<?php
function test()
{
    static $a = 0;
    echo $a;
    $a++;
}
?>

 

I could call this function in one page and it would set its value to 1 and then have a link to the next page which calls the function and it would display 2?

Link to comment
Share on other sites

Try this -  (I haven't tested it)

<?php

class A {
     private static $e = 0;
     public function __construct ($e = null) {
          if (!is_null($e)) A::$e = $e;
     }
     public function getE () { return A::$e; }
}

$e = new A(5);
$f = new A;

echo $f->getE();

Link to comment
Share on other sites

I tested the function out using three separate pages.  One for the class then another to set the value of e to 5 then the third file to get the value of e and it was still 0..I included the class in test2 and test3 pages.

 

Is this not what you meant?

Link to comment
Share on other sites

You need to use sessions or a database/flat file to continue using a value across multiple scripts.

 

Static reads as following:

Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).
Link to comment
Share on other sites

I guess I need to just give up.  I have been trying to figure out a way to catch a error and have it sent to another page without POST or GET methods.  I save the error in a flat file didn't want to read that and wanted to avoid using a database, but I guess whats the difference in this instance.  It would make things a lot simpler.

 

should I use sessions for something like this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.