[web] PHP bug?
I'm not as familiar with PHP classes as I should be, but I believe you've missed a fundamental step.
Maybe try replaceing:
Test::v2('t', false);
with
$MyTest = new Test;
$MyTest::v2('t', false);
or with
$MyTest = new Test;
$MyText->v2('t', false);
The latter is what I use. From my undestanding, -> would call a child class function whereas :: calls the parent function. If there is no parent, perhaps they are equivalent statements.. I've just always used ->. Also, I've written tons of classes this way and have never had a problem.
Maybe try replaceing:
Test::v2('t', false);
with
$MyTest = new Test;
$MyTest::v2('t', false);
or with
$MyTest = new Test;
$MyText->v2('t', false);
The latter is what I use. From my undestanding, -> would call a child class function whereas :: calls the parent function. If there is no parent, perhaps they are equivalent statements.. I've just always used ->. Also, I've written tons of classes this way and have never had a problem.
@Cygnus_X: I hate to tell you this, but all of that is incorrect. This has nothing to do with inheritance. Also: you do not need an instantiated object to call a static class function. It's just like C++.
Also, I've written tons of classes this way and have never had a problem. [/quote]
Now I'm worried for you . Make sure you understand static class functions versus regular class functions called on objects! No hard feelings, ok?
@Supah Fly: No, this isn't a bug. Change this line:
if (!v1($k))
to:
if (!Test::v1($k))
Explained:
v1() is not a defined function. It is trying to look in global scope and it obviously doesn't exist. Simply add the correct scope operator.
There are no hard feelings. I am not a CS major, juts a hobbyist. I've never needed to differentiate between public and private methods in php.
There are no hard feelings. I am not a CS major, juts a hobbyist. I've never needed to differentiate between public and private methods in php.
i'm not a CS major either
This isn't about public/private. It's about static/non-static.
See here for a quick lesson
What is the advantage of having a static class?
It's not about a static class [?], it's about static class members.
These members can be accessed without needing to instantiate a class, their value is also kept during the running cycle of the program.
C++ related but easy to understand for other languages http://www.exforsys.com/tutorials/c-plus-plus/static-functions.html
Cheers
I see. I'm guessing you'd want to do this to save a call to _constructor()? I can see why I've never had a need for this...
...What is the advantage of having a static class?...I'm guessing you'd want to do this to save a call to _constructor()?...
Oh no, not for saving an anyway cheap function call. Static members and methods are used in software engineering.
You as the developer of a class know how it is implemented. The next developer, who just uses this class, don't need to know the internals. A factory method is a perfect example for a static method. The method is implemented with the knowledge how to create an instance of a specific inherited class. The user just needs to know the static factory method, which creates an instance of that specific class.
Long story short: static is useful when you need data or functions that pertain to the class as a whole but not to any particular object.
For instance, let's say i've got a database app and a class called "Thing". Let's say the Things have a "rank". I can have a static class function that does something like:
You can then use it like so:
$highest_thing = Thing::GetHighestThing();
I use static functions all the time. They are definitely worth knowing about!
For instance, let's say i've got a database app and a class called "Thing". Let's say the Things have a "rank". I can have a static class function that does something like:
class Thing {
static function GetHighestThing() {
// database looks up highest ranking Thing and returns Thing object
}
}]
You can then use it like so:
$highest_thing = Thing::GetHighestThing();
I use static functions all the time. They are definitely worth knowing about!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement