6 Replies - 200 Views - Last Post: 11 November 2013 - 03:53 PM Rate Topic: -----

#1 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 673
  • View blog
  • Posts: 2,962
  • Joined: 03-December 12

calling methods in the same class

Posted 11 November 2013 - 10:11 AM

Having an issue with trying to get this section of code to work

$this->email_code = $this::setEmailCode($email);
$this->email_code = $this->setEmailCode($email); //have also tried



Method I am trying to call

	/**
	*		Create unique retrival code for Updating database or unsubscribing
	*/
	function setEmailCode($email)
	{
		$this->email_code = sha1($email + microtime());	
	}
	
	function getEmailCode()
	{
		return $this->email_code;
	}


I have verified $email does have a value but, sets as ""

Any advise?

Is This A Good Question/Topic? 0
  • +

Replies To: calling methods in the same class

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: calling methods in the same class

Posted 11 November 2013 - 10:31 AM

Quote

I have verified $email does have a value but, sets as ""

of course. what do you expect when you cast a string to a number to use for addition?

what you’re looking for is the concatenation operator .
Was This Post Helpful? 0
  • +
  • -

#3 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 673
  • View blog
  • Posts: 2,962
  • Joined: 03-December 12

Re: calling methods in the same class

Posted 11 November 2013 - 10:38 AM

I have tried that too. still displays nothing when called.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: calling methods in the same class

Posted 11 November 2013 - 10:42 AM

have you anything more you could tell us? from that bit that’s pretty much all we can get.
Was This Post Helpful? 0
  • +
  • -

#5 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 673
  • View blog
  • Posts: 2,962
  • Joined: 03-December 12

Re: calling methods in the same class

Posted 11 November 2013 - 01:34 PM

View Postastonecipher, on 11 November 2013 - 10:11 AM, said:

Having an issue with trying to get this section of code to work

$this->email_code = $this::setEmailCode($email);
$this->email_code = $this->setEmailCode($email); //have also tried


The second one comes closest to working. The problem was the assignment. When I removed the 'stuff' to the left of the equals sign.

Problem solved.
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: calling methods in the same class

Posted 11 November 2013 - 03:41 PM

Yep, that would make sense. Consider what the method does: it's a setter method, whose purpose is to set an attribute. It sets it, and then returns nothing. If you then assign it's return value to the variable it has already set, you'll be overwriting that set value by the return value, which is a null.


About the $this:: vs $this-> syntax. The second one is always the correct usage. The :: operator is for static calls, and should be called either with a class name or the self keyword.
public function doStuff() {
    // Calls a method of this object instance.
    $this->doMoreStuff();

    // Calls a static method of the class this object
    // instance is built from.
    self::doStaticStuff();
}


Technically you can do $this::doStaticStuff() as well, but it's not the "correct" way to do it. To be perfectly honest, in my opinion, it should throw an error. Not sure why the PHP devs chose not to do that.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,833
  • Joined: 12-December 12

Re: calling methods in the same class

Posted 11 November 2013 - 03:53 PM

View PostAtli, on 11 November 2013 - 10:41 PM, said:

Technically you can do $this::doStaticStuff() as well, but it's not the "correct" way to do it. To be perfectly honest, in my opinion, it should throw an error. Not sure why the PHP devs chose not to do that.

Perhaps they just borrowed the idea from other languages? Python for example, as I recall. This would be typical for PHP.

If a static-method is being called from an instance then perhaps it shouldn't be static. So, I agree, an error, or at least an informative warning, would be better. Preferably an error ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1