Jump to content
  • Advertisement
Sign in to follow this  
Supah Fly

[web] PHP bug?

This topic is 2705 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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++.

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:

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.

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

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 :D

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

See here for a quick lesson

Share this post


Link to post
Share on other sites

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

Share this post


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

Share this post


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

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:

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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!