• You are not logged in. | Login

Post a reply

  • Index
  •  » Help me!
  •  » How can you benefit using :: for access to methods

February 26, 2007 10:43 am

Keeper
Member
Ranks

How can you benefit using :: for access to methods

Could you possibly explain on examples what the following way of methods’ access is used for? There is class A and there is MyMethod in it which can be called so:
$Object = new A();
$Object->MyMethod();
Or so
A::MyMethod() at this you are to replace all the applying to this class’s data from method through $this with self::

What is the practical advantage of such method? What is it suitable for?


 

 

February 26, 2007 10:46 am

Re: How can you benefit using :: for access to methods

And what is not clear from the documentation?


 

 

February 26, 2007 10:49 am

n00bphp
Member
Ranks

Re: How can you benefit using :: for access to methods

$Object = new A();$Object->MyMethod(); - created an object
A::MyMethod() - didn’t create any object


 

 

February 26, 2007 10:53 am

mmwfan
Member
Ranks

Re: How can you benefit using :: for access to methods

Keeper
$Object->MyMethod(); calls method of an OBJECT
A::MyMethod(); calls static functions of a CLASS

What is the benefit from usage?
Should classes be used as namespaces, for instance?


 

 

February 26, 2007 11:01 am

Keeper
Member
Ranks

Re: How can you benefit using :: for access to methods

And what is not clear from the documentation?

I’ve written it. I cannot understand what the advantage is in comparison with usual method shown as #1.

$Object = new A();$Object->MyMethod(); - created an object
A::MyMethod() - didn’t create any object

And what if I haven’t created an object?

What is the benefit from usage?
Should classes be used as name spaces, for instance?

What do you mean? When is it needed? What tasks are solved this way more effectively?


 

 

February 26, 2007 11:05 am

mmwfan
Member
Ranks

Re: How can you benefit using :: for access to methods

Do you know what namespace is?
In PHP4 this approach compensates a bit lack of namespaces… And what do we need them for?... There are a lot of manuals about it.


 

 

February 26, 2007 11:11 am

Re: How can you benefit using :: for access to methods

$Object = new A();$Object->MyMethod(); - created an object
A::MyMethod() - didn’t create any object

Method 1: create an object and use other methods of this object with its data.
Method 2: don’t create any object, use class as a set of functions, for instance, define a class for handling strings and use single functions only without creating object STR::cut(), STR::dup() etc...


 

 

February 26, 2007 11:14 am

n00bphp
Member
Ranks

Re: How can you benefit using :: for access to methods

Keeper, by object creation some actions occur, for example, constructor is accomplished, place for an object is freed but it’s not always necessary.


 

 

February 26, 2007 11:16 am

Keeper
Member
Ranks

Re: How can you benefit using :: for access to methods

mmwfan,
namespace gains an ability to use methods and variables depending on the context.

In the documentation concerning static methods is written that such a method is used for ability to call method without creating an object. Why shouldn’t we declare all the methods like this? We could use static ones only.

Not many manuals are needed, just a couple of examples are enough to understand – usage of static methods is really justified here.


 

 

February 26, 2007 11:21 am

mmwfan
Member
Ranks

Re: How can you benefit using :: for access to methods

Keeper, do you know the difference between such notions as CLASS and OBJECT? Be honest, please!  grin


 

 

February 26, 2007 11:24 am

Keeper
Member
Ranks

Re: How can you benefit using :: for access to methods

mmwfan, class is a description and object is a concrete specimen of a class possessing the behavior described. But how does it help me?

n00bphp, selmahyekishot, it’s clear now, thanks. By the time I haven’t found any applying for that except for methods archive.


 

 

February 26, 2007 11:31 am

mmwfan
Member
Ranks

Re: How can you benefit using :: for access to methods

Keeper, you won’t see anything as far as this is the main scope of static methods.


 

 

February 26, 2007 11:33 am

n00bphp
Member
Ranks

Re: How can you benefit using :: for access to methods

Keeper, look up in Google expression ‘programming patterns’. Programming language doesn’t matter. And you’ll see another applying of classes.


 

 

February 26, 2007 11:36 am

mastaweb99
Member
Ranks

Re: How can you benefit using :: for access to methods

Keeper, one of the ways to use static methods is different patterns generating objects (such as Singleton, Factory Method and others): direct call of a constructor results into a static call.
$obj = SomeClass::createObject();


 

 

February 26, 2007 11:39 am

monkeydude
Member
Ranks

Re: How can you benefit using :: for access to methods

Keeper,

From practical point of view variant including object creation enables evading of class description. You are to declare interface only. It means we can replace a class every moment and none will mention it. I also asked myself about it when writing a big project. Finally I force myself to evade static methods. And if I have to use them I apply experience I got when writing windows programs. It means I create Application object which initiates everything requiring all singletons and deciding which one should be called. After that it calls the necessary module and transmits all these data to it along with a link to itself. The module proves not to know what class is there – the main thing is that everything functions properly. Even if you need to change singleton it is done in one place only.

Do you want any example? I have such. There are a lot of frameworks now. But none of them suits properly. And so you have to write something on your own. And when you find something better and ready-made you start changing everything in hurry. So I have instilled privilege system into a project. And later I found a good solution within one framework. I extracted necessary files, wrote an adapter and changed one string (name) within Application. That’s all. The project hasn’t mentioned that it wasn’t a class.

Of course, someone may say that we could replace a class with statics as well. However this class is used not in one project only as far as it is a part of a framework. And I risk being abused by my colleagues working over the next project for changing components of a framework.

There are a lot of manuals about it but your own experience is quite a different thing  smile

Having some experience you can always evade overt creating an object in code and use its suspended initialization at the same time.

Last edited by monkeydude (February 26, 2007 11:40 am)


 

 

February 26, 2007 11:56 am

jjjlc1983
Member
Ranks

Re: How can you benefit using :: for access to methods

Class is a description and object is a concrete specimen of a class possessing the behavior described.

It’s wrong. More precisely it’s incomplete and so it’s wrong.

Class is behavior which can be applied to the data set. This behavior is nonsense without data as well as data without any handling is nonsense too.

When we declare a class we declare behavior and data content. When we create a specimen we don’t copy behavior – it’s already described with the class. We create a copy of data set to which a definite behavior may be applied. Class is neither behavior nor data. Class is a total of behavior and data which ‘behave’ somehow within a program. And specimen is concrete data access to which is defined with interface of a class. Each specimen is a separate copy of set of attributes filled with concrete personal meanings. Each specimen contains similar methods, similar attributes’ set but attributes’ value may be different.

Calls with:: cannot be applied to specimen. This means that in fact they don’t refer to class as far as such a call cannot address data of a definite specimen. It’s an ordinary function realized in the namespace of a class. Such calls may be used with static attributes only which are ‘global’ for all the specimens of one class and its successors but don’t refer to concrete specimen.


 

 

February 26, 2007 12:00 pm

Re: How can you benefit using :: for access to methods

If you need some examples, you’d better use this. The difference is visible at once.

<?php
class api {
public function setReturnPath() {
$this->session['returnPath'][$this->mDir] = getenv('REQUEST_URI');
}
}


class module {
public $session;
public $mDir="moduleDir";

public function showReturnPath() {
echo $this->session['returnPath'][$this->mDir];
exit();
}

function __construct() {
global $_SESSION;

$this->session = &$_SESSION;
api::setReturnPath();
$this->showReturnPath();
}
}

?>

 

 

February 26, 2007 12:03 pm

jjjlc1983
Member
Ranks

Re: How can you benefit using :: for access to methods

Thanks a lot.
global $_SESSION; is very impressing  smile


 

 

July 13, 2007 6:00 am

prisharma
Member
Ranks

Re: How can you benefit using :: for access to methods

It is use for reduce the code..........


 

 
  • Index
  •  » Help me!
  •  » How can you benefit using :: for access to methods
  • Actions
  • Top
ITCrimea. Ukraine Web Development Company. Professional Developers and Web Designers Team
Custom Web Designs, Internet Applications, E-Commerce Websites, Interactive Sites, Database-Driven Sites and Services