...

View Full Version : Resolved OOP class call function inside of self



mike182uk
01-15-2010, 11:19 PM
hi there

i am currently learning php, and im struggling with somehting, due to my lack of knowledge.

how do i call a class function inside the class




class myclass
{
function func1()
{
return "Hello World!";
}

function func2()
{
return func1(); //call func1 from this class
}

}



this way doesnt seem to work. do i have to instaniate the class first?

in other languages this normally works.

timgolding
01-15-2010, 11:28 PM
you have to call it with $this

so it would be



class myclass
{
function func1()
{
return "Hello World!";
}

function func2()
{
return $this->func1(); //call func1 from this class
}

}

mike182uk
01-15-2010, 11:34 PM
hey thanks for the quick reply.

well shouldnt this work?



class myclass
{
function func1()
{
return "Hello World!";
}

function func2()
{
return $this->func1(); //call func1 from this class
}

}

echo myclass::func2();


i get an error:



Fatal error: Using $this when not in object context...

Dormilich
01-15-2010, 11:37 PM
the :: notation is reserved for static methods/properties. or in other words, to use $this, you need an instance of the class (created with new).


// as of PHP 5
class myClass
{
protected static $text = "something";
protected $otherText = " else";

public static function doSomething()
{
echo self::$text;
}

public function doSomethingElse()
{
echo self::$text, $this->otherText;
}
}

// echoes "something"
myClass::doSomething();
// fails
myClass::doSomethingElse();

$mci = new myClass;
// echoes "something else"
$mci->doSomethingElse();

timgolding
01-15-2010, 11:44 PM
hey thanks for the quick reply.

well shouldnt this work?



class myclass
{
function func1()
{
return "Hello World!";
}

function func2()
{
return $this->func1(); //call func1 from this class
}

}

echo myclass::func2();


i get an error:



Fatal error: Using $this when not in object context...


No try this



class myclass
{
function func1()
{
return "Hello World!";
}

function func2()
{
return $this->func1(); //call func1 from this class
}

}

$class = new myclass();
echo $class->func2();

JAY6390
01-15-2010, 11:45 PM
This would also work

class myclass
{
function func1()
{
return "Hello World!";
}

function func2()
{
return myclass::func1(); //call func1 from this class
}

}

echo myclass::func2();

kbluhm
01-15-2010, 11:50 PM
This would also work

class myclass
{
function func1()
{
return "Hello World!";
}

function func2()
{
return myclass::func1(); //call func1 from this class
}

}

echo myclass::func2();

No, you'd have to name methods as static to call them like that. It'd work in PHP4, but 5 would throw a notice or a warning... don't have time to research it.

mike182uk
01-15-2010, 11:50 PM
awesome thank you for your help.

so if i ever want to call something whilst inside the class $this-> always referers to the class?

so if i define a variable in the class i access it with $this->variable rather than $variable?

kbluhm
01-15-2010, 11:53 PM
awesome thank you for your help.

so if i ever want to call something whilst inside the class $this-> always referers to the class?

so if i define a variable in the class i access it with $this->variable rather than $variable?

Unless you're in a static method, which are out of the scope of $this.

timgolding
01-15-2010, 11:53 PM
awesome thank you for your help.

so if i ever want to call something whilst inside the class $this-> always referers to the class?

so if i define a variable in the class i access it with $this->variable rather than $variable?

Yep :)

Dormilich
01-15-2010, 11:55 PM
so if i ever want to call something whilst inside the class $this-> always referers to the class?

it refers to the instance of the class. to refer to the class itself use self:: (inside) or className::


so if i define a variable in the class i access it with $this->variable rather than $variable?
yes (given that you use the class instance). static properties are called via self::$propertyName or className::$propertyName

mike182uk
01-16-2010, 12:00 AM
well thank you all for your help on this. i guess i need to go away and brush up in my oop php. i am coming from a javascript / asp background, so things seem a little wierd. thanks again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum