Answers for "how to make a class in php"

PHP
7

create a class in php

<?php
class Fruit {
  public $name;
  public $color;

  function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  function get_name() {
    return $this->name;
  }
  function get_color() {
    return $this->color;
  }
}

$apple = new Fruit("Apple", "red");
echo $apple->get_name();
echo "<br>";
echo $apple->get_color();
?>
Posted by: Guest on July-22-2020
5

class php

<?php
class Foo {
    public $aMemberVar = 'aMemberVar Member Variable';
    public $aFuncName = 'aMemberFunc';
   
   
    function aMemberFunc() {
        print 'Inside `aMemberFunc()`';
    }
}

$foo = new Foo;

function getVarName()
{
     return 'aFuncName'; 
}

print $foo->{$foo->{getVarName()}}();
Posted by: Guest on April-23-2020
1

php class

var array = [1, 2, 3, 4, 5, 6],
    s = 0,
    p = 1,
    i;
for (i = 0; i < array.length; i += 1) 
   {
    s += array[i];
    p *= array[i];
    }
console.log('Sum : '+s + ' Product :  ' +p);
Posted by: Guest on April-23-2021
-2

php class

var numbers = [10, 20, 30, 40] // sums to 100var sum = 0;for (var i = 0; i < numbers.length; i++) {  sum += numbers[i]}
Posted by: Guest on April-23-2021

Code answers related to "how to make a class in php"

Browse Popular Code Answers by Language