Hello Everybody,
I’m not even sure how to search for the answer to the question but what does the colon mean and how is it used ::
example: $test::test
Hello Everybody,
I’m not even sure how to search for the answer to the question but what does the colon mean and how is it used ::
example: $test::test
the double-colon sign is used for a static call.
<?php
class test
{
const george = 'G';
public static function getName()
{
return 'Mike!';
}
}
// we can have
$testObj = new test;
echo $testObj::george.'<br>';
echo $testObj::getName().'<br>';
// or, same thing
echo test::george.'<br>';
echo test::getName().'<br>';
Hi Vectorialpx,
thanks for the reply back.
I seen example where an arrow is used to point to an objects example $testObj->george, how is this different?
question 2,
you have test::george, is this mean that you dont have to create an object?
Your second question answers the first
I hope the next code will answer better
<?php
class human
{
// by convention, constants will be uppercase
// constants cannot be changed
const FIRST_NAME = 'G';
// ---
// this is a static property of the object
// properties (are like variables) can be changed
// moreover, static properties will remain and can be used globally
// human::$name will be available everywhere
// and once is set, will remain there
// (in one call, do not confuse it with sessions)
public static $name = 'Maria';
// ---
// this is a simple property of the object
protected $gender = 'female';
// ---
// static variables can be changed and
public static function setName($n)
{
self::$name = $n;
}
// ---
// get full name of the
public static function getFullName()
{
// invalid code:
// return $this->gender;
// because we may not have an instance of the object
//
// valid code
return self::FIRST_NAME.' '.self::$name;
}
// ---
public function getFullDetails()
{
return [
// you can use static calls into your methods
'name' => self::getFullName(),
'gender' => $this->gender,
];
}
}
$hClass = new human;
$hClass::setName('Mark');
var_dump(human::getFullName());
var_dump($hClass->getFullDetails());
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.