php create instance of class code example

Example 1: php define class

class Bike {
    	function Bike() {
            $this->type = 'BMX';
    }
}

$blackSheep = new Bike();

print $blackSheep->type;

Example 2: 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()}}();

Example 3: php object

$obj = [
  "example1" => 1,
  "example2" => "two",
  "example3" => [
    "example4": true
  ]
]

Tags:

Php Example