Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,821 answers

573 users

How to define and use class properties in PHP

3 Answers

0 votes
class Test 
{
    public $pr = "Class property";
}

$obj = new Test();

echo $obj->pr;

echo "<br />";

var_dump($obj);
 
/*
run:

Class property
object(Test)#1 (1) { ["pr"]=> string(14) "Class property" } 

*/

 



answered Oct 22, 2015 by avibootz
edited Oct 22, 2015 by avibootz
0 votes
class Test 
{
    public $pr = "Class property";
  
    public function setProperty($val)
    {
        $this->pr = $val;
    }
  
    public function getProperty()
    {
        return $this->pr;
    }
}
 
$obj = new Test();
 
echo $obj->getProperty(); 
 
echo "<br />";
  
$obj->setProperty("New property value"); 
  
echo $obj->getProperty(); 

echo "<br />";

var_dump($obj);
  
/*
run:
 
Class property
New property value
object(Test)#1 (1) { ["pr"]=> string(18) "New property value" } 
 
*/

 



answered Oct 22, 2015 by avibootz
0 votes
class Test 
{
    public $pr = "Class property";
  
    public function setProperty($val)
    {
        $this->pr = $val;
    }
  
    public function getProperty()
    {
        return $this->pr;
    }
}
 
$obj1 = new Test;
$obj2 = new Test;
 
echo $obj1->getProperty();
echo "<br />";
echo $obj2->getProperty();
 
$obj1->setProperty("New property value - first instance");
$obj2->setProperty("New property value - second instance");
 
echo "<br />";
echo $obj1->getProperty();
echo "<br />";
echo $obj2->getProperty();
  
/*
run:
 
Class property
Class property
New property value - first instance
New property value - second instance 
 
*/

 



answered Oct 22, 2015 by avibootz

Related questions

2 answers 201 views
201 views asked Sep 7, 2017 by avibootz
1 answer 173 views
1 answer 172 views
172 views asked May 30, 2016 by avibootz
1 answer 223 views
1 answer 204 views
...