Sign in to follow this  
Supah Fly

[web] PHP bug?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
@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++.

[quote]Also, I've written tons of classes this way and have never had a problem. [/quote]

Now I'm worried for you :huh:. 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:

[b]if (!v1($k[/b][b]))[/b]

to:
[b]
[/b][b]if (!Test::v1($k))[/b]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='Cygnus_X' timestamp='1298300399' post='4777033']
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.
[/quote]

i'm not a CS major either :D

This isn't about public/private. It's about static/non-static.

[url="http://php.net/manual/en/language.oop5.static.php"]See here for a quick lesson[/url]

Share this post


Link to post
Share on other sites
[quote name='Cygnus_X' timestamp='1298382048' post='4777514']
What is the advantage of having a static class?
[/quote]

It's not about a static class [?], it's about static class [b]members[/b].
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

Share this post


Link to post
Share on other sites
[quote name='Cygnus_X' timestamp='1298390574' post='4777554']...What is the advantage of having a static class?...I'm guessing you'd want to do this to save a call to _constructor()?...
[/quote]

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.

Share this post


Link to post
Share on other sites
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:

[code]class Thing {
static function GetHighestThing() {
// database looks up highest ranking Thing and returns Thing object
}
}][/code]

You can then use it like so:

$highest_thing = Thing::GetHighestThing();


I use static functions all the time. They are definitely worth knowing about!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this