Setting public class variables using php with example

Setting public class variables using php with example

In this post we will show you Setting public class variables, hear for Setting public class variables we will give you example for implement.

For Setting public class variables, Class part factors are called “properties”. You may likewise observe them alluded to utilizing different terms, for example, “qualities” or “fields”, however for the reasons for this reference we will utilize “properties”. They are characterized by utilizing one of the catchphrases public, protected, or private, trailed by a typical variable revelation. This presentation may incorporate an instatement, however this introduction must be a steady esteem – that is, it must have the capacity to be assessed at accumulate time and should not rely upon run-time data keeping in mind the end goal to be assessed.

See Visibility for more data on the implications of open, ensured, and private.

Note:

In request to keep up in reverse similarity with PHP 4, PHP 5 will in any case acknowledge the utilization of the watchword var in property statements rather than (or notwithstanding) public, protected, or private. Be that as it may, var is never again required. In forms of PHP from 5.0 to 5.1.3, the utilization of var was considered censured and would issue an E_STRICT cautioning, however since PHP 5.1.3 it is never again belittled and does not issue the warning.

If we announce a property utilizing var rather than one of public, protected, or private, at that point PHP 5 will regard the property as though it had been proclaimed as public.

Inside class strategies non-static properties might be gotten to by utilizing - > (Object Operator): $this->property (where property is the name of the property). Static properties are gotten to by utilizing the :: (Double Colon): self::$property. See Static Keyword for more data on the distinction amongst static and non-static properties.

The pseudo-variable $this is accessible inside any class technique when that strategy is called from inside a protest setting. $this is a reference to the calling object (as a rule the protest which the technique has a place, yet conceivably another question, if the strategy is called statically from the setting of an optional protest).

Example #1 Setting public class variables

class Onlinecodeclass
{
private $test_variables = "Setting public class variables example";

public function set_test_variables($test_variables) {
$this->test_variables = $test_variables;
}
public function get_test_variables() {
return $this->test_variables;
}

function some_process()
{
echo $this->get_test_variables();
}
}

$Onlinecodeclass = new Onlinecodeclass();

$Onlinecodeclass->set_test_variables("New value for Setting public class variables example");

$Onlinecodeclass->some_process();




Example #2 :: from outside the class definition

hear is another Example for Setting public class variables with defined variables values outside of class using outside the class definition. This special keywords parent, self and static is used to methods from inside the class definition or access properties.

<?php
class Onlinecodeclass
{
const CONST_VALUE = 'Onlinecodeclass value';
}

$setclassname = 'Onlinecodeclass';
echo $setclassname::CONST_VALUE;
// As per PHP 5.3.0

echo Onlinecodeclass::CONST_VALUE;
?>

Example #2 :: from outside the class definition

hear is another Example for Setting public class variables with defined variables values outside of class using outside the class definition.

At the point when a broadening class supersedes the guardians meaning of a strategy, PHP won’t call the parent’s technique. It’s up to the expanded class on regardless of whether the parent’s technique is called. This additionally applies to Constructors and Destructors, Overloading, and Magic technique definitions.

<?php
// Onlinecode class
class Onlinecodeclass extends MyOnlinecodeClass
{
public static $my_static_variables = 'Onlinecode var';

public static function set_colon_double() {
echo parent::CONST_VALUE . "\n";
echo self::$my_static_variables . "\n";
}
}

$set_classname = 'Onlinecodeclass';
$set_classname::set_colon_double();
// As per PHP 5.3.0

Onlinecodeclass::set_colon_double();
?>

Example #3 :: from outside the class definition

hear is another Example for Setting public class variables with defined variables values outside of class using outside the class definition.

<?php
// My Onlinecode Class
class MyOnlinecodeClass
{
// set protected function
protected function myOnlinecodeFunc() {
echo "MyOnlinecodeClass::myOnlinecodeFunc()\n";
}
}
// Other Onlinecode Class
class OtherOnlinecodeClass extends MyOnlinecodeClass
{
// set Override parent's definition
public function myOnlinecodeFunc()
{
// But hear we still call parent function
parent::myOnlinecodeFunc();
echo "OtherOnlinecodeClass::myOnlinecodeFunc()\n";
}
}

$set_classname = new OtherOnlinecodeClass();
$set_classname->myOnlinecodeFunc();
?>




Example #5 Setting public class variables for property declarations

hear is another Example for Setting public class variables with defined variables values outside of class using declarations of property .

<?php
// Setting public class variables example
class Onlinecodeclass
{
// For valid as of PHP 5.6.0:
public $variables1 = 'Onlinecode ' . 'Example';
// For valid as of PHP 5.3.0:
public $variables2 = <<<EOD
Onlinecode Example
EOD;

// For Only valid as of PHP 5.6.0:
public $variables3 = 1 + 7;
// For declarations invalid property ::
public $variables4 = self::setStaticMethod();
public $variables5 = $myvariables;

// For declarations valid property ::
public $variables6 = Onlinecode_Constant;
public $variables7 = array(true, false);

// For Only valid as of PHP 5.3.0:
public $variables8 = <<<'EOD'
Onlinecode Example
EOD;
}
?>

Note:

There are some nice functions to handle classes and objects. You might want to take a look at the Class/Object Functions.
As of PHP 5.3.0 heredocs and nowdocs can be used in any static data context, including property declarations.




Example #6 Setting public class variables of using a nowdoc to initialize a property

hear is another Example for Setting public class variables with defined variables values outside of class using a nowdoc to initialize a property.

<?php
// Setting public class variables example
class Onlinecodeclass {
// For Only PHP 5.3.0
public $variables = <<<'EOT'
variables
EOT;
public $variables = <<<EOT
variables
EOT;
}
?>

Example #7 Setting public class variables

hear is another Example for Setting public class variables with defined variables values outside of class.

<?php
// Setting public class variables example
class Onlinecodeclass
{
public $variables;

function some_process()
{
// echo variables data
echo $this->variables;
}
}

$Onlinecodeclass = new Onlinecodeclass();
$Onlinecodeclass->variables = "Onlinecode variable";
$Onlinecodeclass->some_process();
//It's print "Onlinecode variable"
?>




Hope this code and post will helped you for implement Setting public class variables. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  44  =  48

We accept paid advance and paid guest Posting on our Site : Contact US