PHP: call a function from a function in a class?

I'm having trouble calling a function within a class. For Example: (I'm writing from my iPhone, so I'm abbreviating a bit)

<? Php
class MyClass {

__construct() {}

public function createField() {
// create a 300 Square Grid with 40x40px Squares }

public function generateSpecialSquares() {
// some squares are better than others oh my!

setPosition (0, 0);
}

public function setPosition($x, $y) {
// display any sprite at $X,$Y on the Grid }

?>

I've tried the following (each time receiving a certain error)

MyClass->setPosition(0, 0)
$this->setPosition(0, 0)
setPosition(0, 0)

I can't seem to access my functions from within my class. Any help would be appreciated

It seems like you didn't instantiate an object.

This is instantiating an object.
$blah = new MyClass();

$blah is now an object of class MyClass.

Then you call the setPosition method like this
$blah->setPosition(0, 0)

By the way, you look cute. I would like to date you.

It seems like you're asking how to make the call to the MyClass function setPosition() from within another MyClass function called generateSpecialSquares().In order to do this, you use the $this-> prefix, so the call would look like:

$this->setPosition (0, 0);

But, as the other post suggest, you must first instantiate your object in order to call any functions within MyClass. Only functions declared as static can be called without instantiating an object. The call in the static case would look like:

MyClass: generateSpecialSquares
MyClass: setPosition();

So the following is what would your code look like in the NON-STATIC CASE:

<? Php
class MyClass {

function __constructor(){

}

public function createField() {
// create a 300 Square Grid with 40x40px Squares }
}

public function generateSpecialSquares() {
// some squares are better than others oh my!

$this->setPosition();
}

public function setPosition($x = 0, $y = 0) {
// display any sprite at $X,$Y on the Grid }
echo("Position: ({$x},{$y})");
}
} // *** End Class

// *** Instantiate object and call its function.
$grid = new MyClass();
$grid->generateSpecialSquares();
?>

***…
In the STATIC CASE, it would look like:
(NOTE: the use of the static keyword in
front of the affected functions)
***…

<? Php
class MyClass {

function __constructor(){

}

public function createField() {
// create a 300 Square Grid with 40x40px Squares }
}

static function generateSpecialSquares() {
// some squares are better than others oh my!

myclass: setPosition(5, 4);
}

static function setPosition($x = 0, $y = 0) {
// display any sprite at $X,$Y on the Grid }
echo("Position: ({$x},{$y})");
}
} // *** End Class

// *** Make call to static function.
MyClass: generateSpecialSquares();
?>

***…
NOTE:
***…
I also set the parameters to default to zero in the setPosition() function. This way, if you call the function with no parameters, you get position where x = 0 and y = 0.

  • Will it show up as a missed call if i call someone by accident for half a ring? So I accidentally called my ex on his iphone, but I quickly realized my mistake and hung up after a half ring. Like you know how when you're calling someone it rings, I hung up after the first riiiing. My question is, will it show up on his phone as a missed call? I tested it out on 2 friends. One iphone 5 and iphone 4. They both told me they didn't get a call. Am I in the clear?
  • What to prepare for PHP interview? I'm called at the company on 11th Jan. I got an email and it was written that the opening is for PHP and HTML, testing and designing, iphone and flash. So I guess if I tell them that I'm interested in php job as a fresher so they may stick their questions on php and other related subjects. So I WANNA KNOW WHAT TO PREPARE TO BEAT THIS INTERVIEW? Please tell me so that i can get this job.
  • IPhone Application IDE for PHP developer? I want to develop my 1st iPhon App, which software client/IDE should I use: I'm a PHP developer. Also need a step by step guide, if possible any entry label PDF document would have been very helpful.
  • IPhone 6s Plus, how do you mute a call while on call? (not how to mute ringer)? I have a new iPhone 6s Plus and need to listen in on conference calls without other people on the call hearing me type in the background or the kids, so how do I Mute the sounds on my end so I can listen in without disturbing the others on the call? My current phone keypad on the iPhone 6s Plus only shows 1-9,0 *, and # buttons.